Technology. People. Progress.
Development

Add field notifications and register an OnSave handler

Validate phone and email fields with JavaScript in Dynamics 365.

Add field notifications and register an OnSave handler

Give error notification in Dynamics 365 at field level using setNotification

In Dynamics 365, We can use the JavaScript method setNotification to give errors at the field level. These new methods are very useful in that developers can easily give errors at field level i.e clearly showing on which field error occurred rather than showing all errors at the top of the CRM Forms

Scenario

  • We have a requirement to give an error on field Level i.e. phone field on the account entity, if the length of the phone number is greater than 10.
  • On Save of account entity, JavaScript will execute and check the phone number length and give an error if the length is greater than 10.
  • JavaScript (Event Handler) will be added to the Account entity and On Save Event. So, the JavaScript function will trigger on the Save of account.
Screenshot from the DVMSKE tutorial
Scenario — screenshot 1 View full size

Syntax

  • Below is the latest syntax available for set Notification at field level in Dynamics 365 Online using JavaScript.
Code example
 formContext.getControl(arg).setNotification(message,uniqueId);
Example:
formContext.getControl("telephone1").setNotification("Phone nuber cannot be greater than 10","errorphonelength");

Set Notification

NameTypeRequiredDescription
messageStringYesThe message to display.
uniqueIdStringNoThe ID to use to clear this message when using the clearNotification method.
  • Below is the latest syntax available for clear Notification at field level if required in Dynamics 365 Online using JavaScript.
Code example
formContext.getControl(arg).clearNotification(uniqueId);
Example:
formContext.getControl("telephone1").clearNotification("errorphonelength");

Clear Notification

NameTypeRequiredDescription
uniqueIdStringNoThe ID to use to clear a specific message that was set using setNotification or addNotification. If the uniqueId parameter isn’t specified, the currently displayed notification will be cleared.

SetNotification Example

Below is the JavaScript function “phoneLengthErrorOnSaveOfAccount”

  • 1st create form context from execution context passed as 1st parameter
  • Get the phone field value, if field is not null
  • Get the length of phone field value.
  • Give error notification if phone field value is greater than 10
Code example
function phoneLengthErrorOnSaveOfAccount(executionContext)
{
    debugger;
    var formContext=executionContext.getFormContext();
    var phoneValue=formContext.getAttribute("telephone1").getValue;
    if(phoneValue!=null)
    {
        var phonValLength=phoneValue.length;
        if(phonValLength>10)
        {
            formContext.getControl("telephone1").setNotification("Phone nuber cannot be greater than 10","errorphonelength");

        }
        
    }
}

How to Register JavaScript on OnSave Event of Form

  • Create a New Solution and Name it appropriately, In your solution, add account entity then select  account form.
  • From component section select account entity added and open Form editor
  • From Form editor, click on Form Properties.

How to Register JavaScript on OnSave Event of Form — screenshot 2
How to Register JavaScript on OnSave Event of Form — screenshot 2 View full size

  • Either Create new Web Resource or add JavaScript Function to existing Web Resource.

How to Register JavaScript on OnSave Event of Form — screenshot 3
How to Register JavaScript on OnSave Event of Form — screenshot 3 View full size

  • In the event handler, in control select the “form” field and event as onSave. Click on add.

How to Register JavaScript on OnSave Event of Form — screenshot 4
How to Register JavaScript on OnSave Event of Form — screenshot 4 View full size

  • In the library select the library where you add your code. And in function copy your js function. And check both the checkboxes. Click on ok.

How to Register JavaScript on OnSave Event of Form — screenshot 5
How to Register JavaScript on OnSave Event of Form — screenshot 5 View full size

  • Click on save and publish. Wait until publish.

TEST FIELD LEVEL ERROR : OUTPUT

Error on field Level i.e. phone field on the account entity, if the length of the phone number is greater than 10.

TEST FIELD LEVEL ERROR : OUTPUT — screenshot 6
TEST FIELD LEVEL ERROR : OUTPUT — screenshot 6 View full size

Need help applying this to your business?

Talk to our team about your systems and next step.

Talk to our team
Open original image