Set Form Notifications With Webresource In Dynamics CRM

Introduction

In Dynamics 365 CRM to show form notifications, web resources of type JavaScript was used. Form context UI related client API reference can be used. As an example, contact form and contact entity record used to set show form notifications using setFormNotification.

Step 1

Login to the required environment and select required solution [Contact Customizations Solution in this case] as shown in the below figure.

Set Form Notifications with Webresource in Dynamics CRM

Step 2

After Step 1, select contact web resource in solution and click on Edit as shown in the below figure.

Set Form Notifications with Webresource in Dynamics CRM

Step 3

After Step 2, below code will be written on any function, here handleOnLoad local function inside webresource to show information notification. We use formcontext.ui as base object along with setFormNotification which requires few arguments message, level, unique id to be passed with the following syntax

formContext.ui.setFormNotification(message, level, uniqueId);

As an example to show information message on contact form below code can be used

var firstname= formContext.getAttribute("firstname").getValue();
console.log('First name-'+firstname);
formContext.ui.setFormNotification(firstname+" : INFORMATION notification.", "INFO");

Step 4

After Step 3, to show Warning notification below code can be used

formContext.ui.setFormNotification("Mobile Phone number not filled - Warning notification.", "WARNING");

Step 5

After Step 4, to show error notification condition here is if at all title field is not filled then

var jobtitle = formContext.getAttribute("jobtitle").getValue();
console.log('job title-' + jobtitle);
if (jobtitle == null) {
    formContext.ui.setFormNotification("Job title Mandatory- Error notification.", "ERROR");
}

Step 6

After Step 5, entire code looks like below to show all kinds of notifications

var formContext = executionContext.getFormContext();
if (formContext !== null && formContext != 'undefined') {
    var firstname = formContext.getAttribute("firstname").getValue();
    console.log('First name-' + firstname);
    formContext.ui.setFormNotification(firstname + " : INFORMATION notification.", "INFO");
    formContext.ui.setFormNotification("Mobile Phone number not filled - Warning notification.", "WARNING");
    var jobtitle = formContext.getAttribute("jobtitle").getValue();
    console.log('job title-' + jobtitle);
    if (jobtitle == null) {
        formContext.ui.setFormNotification("Job title Mandatory- Error notification.", "ERROR");
    }
}

Step 7

After Step 6, save web resource and publish it and then open contact record and observe. After loading the record all the notifications will be shown in the notification panel starting from information, warning, error levels as shown in the below figure

Set Form Notifications with Webresource in Dynamics CRM

Note

  1. Make sure to publish all customizations and upload JavaScript (js) file.
  2. Here I have concentrated more on showing notifications only, we can extend it by restricting to save record until error notification [Title] was filled in the above screenshot.
  3. Microsoft documentation found here

Conclusion

In this way, one can easily use set form notifications in web resource to show notifications in CRM Forms for all entities easily.