Show Notifications Beside CRM Field With Web Resource In Dynamics CRM

Introduction

In Dynamics 365 CRM we can  show notifications beside CRM Field clearly on which field error occurred rather than showing all errors at the top of the CRM Forms. Web resources of type JavaScript were used to build this. As an example, contact form and contact entity record were used to show error notifications beside title field using set Notification. If the title was not mentioned on the contact form then error notification will be shown.

Step 1

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

Show Notifications Beside CRM Field with Web resource in Dynamics CRM

Step 2

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

Show Notifications Beside CRM Field with Web resource in Dynamics CRM

Step 3

After Step 2, in contact webresource write logic on the field jobtitle by using getcontrol in formcontext. A variable will be assigned like below

var jobtitlecontrol=formContext.getControl("jobtitle");

As shown in the below figure

Show Notifications Beside CRM Field with Webresource in Dynamics CRM Figure 3

Step 4

After Step 3, in case job title is not present then in order to provide error beside title field we have to use set Notification which expects a couple of parameters, message and uniqueid

formContext.getControl(arg).setNotification(message,uniqueId);
as
jobtitlecontrol.setNotification("Job title Mandatory- Error notification.","jobtitlecontrolnotification");

As shown in the below figure

Show Notifications Beside CRM Field with Webresource in Dynamics CRM Figure 4

Step 5

After Step 4, the final code looks like this under any method which is called under on change of title field and contact form.

function jobtitlemandatorylogic(executionContext) {
    var formContext = executionContext.getFormContext();
    if (formContext !== null && formContext != 'undefined') {
        var jobtitle = formContext.getAttribute("jobtitle").getValue();
        var jobtitlecontrol = formContext.getControl("jobtitle");
        console.log('job title-' + jobtitle);
        if (jobtitle == null) {
            formContext.ui.setFormNotification("Job title Mandatory- Error notification.", "ERROR", "JobTitleMandateNotification");
            jobtitlecontrol.setNotification("Job title Mandatory- Error notification.", "jobtitlecontrolnotification");
        } else {
            formContext.ui.clearFormNotification("JobTitleMandateNotification");
            jobtitlecontrol.clearNotification("jobtitlecontrolnotification");
        }
    }
}

As shown in the below figure

Show Notifications Beside CRM Field with Web resource in Dynamics CRM

Step 6

After Step 5, save and publish webresource and open contact record. If not title is present then one can observe error title beside that field as shown in the below figure

Show Notifications Beside CRM Field with Web resource 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 at field level, and the above function should be called from onchange event of contact title field & contact onload form event.
  3. Microsoft documentation found here

Conclusion

In this way, one can easily use show notifications in web resources beside CRM Entity field other than showing all the errors at the top of the form as form level easily.


Similar Articles