Enhance Dynamics 365 Forms with Custom Notifications

Form notifications in Dynamics 365 CRM offer a straightforward and effective method to communicate crucial information to users directly within entity forms. These notifications can be leveraged to display warnings, errors, or informational messages, aligning with specific business requirements. By employing JavaScript Web Resources, developers gain the ability to dynamically show or hide these notifications as users interact with records, whether opening, updating, or saving them. This article outlines the process of implementing form notifications using JavaScript Web Resources in Dynamics 365 CRM.

Why Use Form Notifications?

Form notifications empower organizations to:

Prerequisites

Before you begin, ensure you have the following:

Step 1: Create a JavaScript Web Resource

Navigate to the customization area in Dynamics 365:

  1. Go to Advanced Settings.
  2. Select Customizations.
  3. Click on Customize the System.
  4. Choose Web Resources.

Create a new JavaScript Web Resource and name it new_FormNotifications.js. Add the following code:


function showFormNotification(executionContext) {
    var formContext = executionContext.getFormContext();
    formContext.ui.setFormNotification(
        "Please verify all mandatory fields before saving.",
        "INFO",
        "notification1"
    );
}

Save and publish this Web Resource.

Understanding setFormNotification()

Dynamics 365 provides the following syntax for setting form notifications:


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

The parameters are defined as follows:

Step 2: Add the Web Resource to the Form

Open the entity form where you want to implement the notification:

  1. Click on Form Properties.
  2. Add the JavaScript Web Resource you just created (new_FormNotifications.js).
  3. Under the Form Libraries section, select the uploaded script.
  4. Register the function showFormNotification.
  5. Select the OnLoad event for the form.
  6. Ensure that the option to Pass execution context as first parameter is enabled.

Save and close the form properties.

Step 3: Publish Customizations

After adding the JavaScript Web Resource and configuring the form events:

  1. Save the form.
  2. Click on Publish All Customizations.

Now, open a record of that entity to test the notification. You should see the message displayed at the top of the form when it loads.

Notification Types

Dynamics 365 supports three distinct notification levels:

Information Notifications

Use the INFO level for general messages.


formContext.ui.setFormNotification(
    "Record loaded successfully.",
    "INFO",
    "infoNotification"
);

Warning Notifications

Use the WARNING level to alert users about potential issues or incomplete data.


formContext.ui.setFormNotification(
    "This account has incomplete information.",
    "WARNING",
    "warningNotification"
);

Error Notifications

Use the ERROR level for critical issues that prevent a user from proceeding or saving correctly.


formContext.ui.setFormNotification(
    "Please complete all required fields.",
    "ERROR",
    "errorNotification"
);

Clearing Form Notifications

Notifications can be removed once they are no longer relevant. This is typically done when a validation passes or a condition is resolved. You use the clearFormNotification() method for this purpose, providing the uniqueId you assigned when creating the notification.


formContext.ui.clearFormNotification("notification1");

Here's an example function to clear a notification:


function clearNotification(executionContext) {
    var formContext = executionContext.getFormContext();
    formContext.ui.clearFormNotification(
        "notification1"
    );
}

Real-Time Example

Consider a scenario where you want to display a warning if the Account Name field is empty. This can be achieved by triggering a JavaScript function on the OnChange event of the Account Name field.


function validateAccount(executionContext) {
    var formContext = executionContext.getFormContext();
    var accountName = formContext.getAttribute("name").getValue();

    if (!accountName) {
        formContext.ui.setFormNotification(
            "Account Name is required.",
            "WARNING",
            "accountValidation"
        );
    } else {
        formContext.ui.clearFormNotification(
            "accountValidation"
        );
    }
}

Register this validateAccount function on the OnChange event of the Account Name field in the form editor.

Best Practices

To maximize the effectiveness of form notifications, consider these best practices:

Benefits of Form Notifications

Implementing form notifications provides several advantages:

Conclusion

Form notifications are a powerful and accessible feature within Dynamics 365 CRM, enabling direct communication of important information to users. By leveraging JavaScript Web Resources and the setFormNotification() method, organizations can provide real-time guidance, enforce critical business rules, and ultimately enhance overall data quality and user productivity. The implementation is relatively straightforward, offering substantial improvements in usability and efficiency for your Dynamics 365 environment.