Dynamics 365 – How to Show Field Notifications | addNotification

Advertisements
Advertisements

📢 In this blog post, I will explain how to display field-level notifications in Dynamics 365 forms and demonstrate the usage of the addNotification() function with code examples. Let’s get started 💻

What is addNotification()?

The addNotification() function is a method of the Control object in the Dynamics 365 Client API. It allows you to display a notification message next to a form control, with an icon indicating the type of notification (error or recommendation) and an optional action button. The notification message can be customized with your own text and actions.

How to use addNotification()?

To use addNotification(), you need to get a reference to the control that you want to display the notification on, using the formContext.getControl() method. Then, you need to pass an object as a parameter to the addNotification() method, with the following properties:

  • messages: An array of strings that contain the message text to display. Only the first string in the array will be displayed in the current release.
  • notificationLevel: A string that defines the type of notification. Valid values are “ERROR” or “RECOMMENDATION”.
  • uniqueId: A string that identifies the notification. You can use this ID to clear the notification later using the clearNotification() method.
  • actions: An optional array of objects that define the action button for the notification. Each object has two properties:
    • message: A string that contains the text for the action button.
    • actions: An array of functions that will be executed when the user clicks on the action button.

Here is an example of using addNotification() to display a recommendation message on a phone number control, with an action button that copies the phone number to the clipboard:

phoneNotification: function (executionContext) {
let formContext = executionContext.getFormContext();
var phoneControl = formContext.getControl("telephone1");
var phoneNumber = formContext.getAttribute("telephone1").getValue();
var notification = {
messages: ["Copy phone number"],
notificationLevel: "RECOMMENDATION",
uniqueId: "phone_notification",
actions: [{
message: "Copy",
actions: [function () {
navigator.clipboard.writeText(phoneNumber);
phoneControl.clearNotification("phone_notification");
}]
}]
};
phoneControl.addNotification(notification);
}
Use cases for addNotification()

The addNotification() function can be used for various scenarios where you want to provide some feedback or guidance to the user based on their input or data on the form. For example, you can use it to:

– Validate user input and display error messages if the data is not valid or complete.
– Suggest alternative values or actions based on business logic or best practices.
– Provide additional information or tips about a field or control.
– Alert the user about potential issues or risks related to their data.

When is addNotification() useful and when is not?

The addNotification() function is useful when you want to display a simple and concise message that relates to a specific control on the form. It can help you improve user experience and data quality by providing timely and relevant feedback.

However, there are some limitations and drawbacks of using addNotification() that you should be aware of:

– The notification message can only be displayed on one control at a time. If you try to display multiple notifications on different controls, only the last one will be visible.
– The notification message can only contain plain text. You cannot use HTML tags, images, links, or other formatting options.
– The notification message has a limited length and space. You should keep your message short and clear, otherwise it might be truncated or overlap with other elements on the form.
– The notification message can interfere with other UI elements on the form, such as labels, icons, tooltips, etc. You should test your notification on different screen sizes and resolutions to ensure it does not cause any layout issues.
– The notification message can be dismissed by the user by clicking on the icon or the action button. You cannot prevent the user from closing the notification or force them to take any action.

Advertisements
Advertisements
Advertisements

Leave a comment

Create a website or blog at WordPress.com