Please wait, loading...

 

Give Error or Warning notification at field level-Phone number Email format-Use setNotification – How to Register JavaScript on OnSave Event of Form in Microsoft Dynamics CRM/CE MSCRM

August 13, 2023

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.
https://dvmske.com/wp-content/uploads/2023/06/Untitled-Diagram.drawio-1-3.png

Syntax

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

Set Notification

Name Type Required Description
message String Yes The message to display.
uniqueId String No The 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.
formContext.getControl(arg).clearNotification(uniqueId);
Example:
formContext.getControl("telephone1").clearNotification("errorphonelength");

Clear Notification

Name Type Required Description
uniqueId String No The 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
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.

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

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

  • 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.

  • 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.

anurag Tyagi