Skip to main content

Notifications

Adding Notifications and Alerts in Dynamics 365 JavaScript using xrm-ex

Introduction

Notifications and alerts in Dynamics 365 improve user experience by providing real-time feedback, validation messages, and warnings. With xrm-ex, developers can efficiently manage field-level, form-level, and global notifications, as well as alert dialogs. This guide demonstrates how to implement these features using xrm-ex for dynamic and interactive forms.

Prerequisites

1. Alerts with Dynamic Height and Width

xrm-ex enhances the standard Dynamics 365 alert dialog by dynamically adjusting its height and width based on the text content—eliminating the need for manual resizing and ensuring better readability, especially for long messages or detailed error logs.

Error Alert
try {
    throw new Error("Something went wrong.");
} catch (error) {
    await XrmEx.openAlertDialog("Error", `Error in ${XrmEx.getFunctionName()}\n${error.message}`);
}
Success Alert
await XrmEx.openAlertDialog("Success", "Operation completed successfully.");

2. Field-Level Notifications

Field notifications highlight specific fields, making it easy to point out errors or provide recommendations directly at the input level.

Add a Notification:
fields.CompanyName.addNotification(
    "Company Name is required.",
    "ERROR",
    "companyNotification"
);
  • Message: Text to display.
  • Level: "ERROR" for errors or "RECOMMENDATION" for suggestions.
  • Unique ID: Used to identify and remove notifications later.
  • Actions (Optional): Add buttons or actions to the notification.
Remove a Notification:
fields.CompanyName.removeNotification("companyNotification");
  • Unique ID: Specifies which notification to remove.
  • Returns true if successful, false otherwise.

3. Form-Level Notifications

Form notifications display messages at the top of the form and remain until explicitly removed.

Add a Form Notification:
XrmEx.Form.addFormNotification(
    "Please review the form before submission.",
    "WARNING",
    "formNotification"
);
  • Level: "ERROR", "WARNING", or "INFO".
  • Unique ID: Required for removal.
Remove a Form Notification:
XrmEx.Form.removeFormNotification("formNotification");

4. Global Notifications

Global notifications appear across the app, outside the context of a single form, making them ideal for displaying system-wide alerts.
Add a Global Notification:
var uniqueId = await XrmEx.addGlobalNotification(
    "System maintenance is scheduled for tonight.",
    "INFO",
    true // Show a close button
);
  • Message: Displayed in the notification bar.
  • Level: "SUCCESS", "ERROR", "WARNING", or "INFO".
  • Close Button: Optional, defaults to false.
Remove a Global Notification:
await XrmEx.removeGlobalNotification(uniqueId);

  • Unique ID: Resolves when successfully cleared.

5. Dynamic Notifications with Conditional Logic

Example: Conditional Validation with Alerts and Notifications
try {
    if (!fields.CompanyName.Value) {
        fields.CompanyName.addNotification(
            "Company Name is required.",
            "ERROR",
            "companyNotification"
        );
        await XrmEx.openAlertDialog("Validation Error", "Please enter a Company Name.");
    } else {
        fields.CompanyName.removeNotification("companyNotification");
        await XrmEx.openAlertDialog("Success", "Company Name is valid.");
    }
} catch (error) {
    await XrmEx.openAlertDialog("Error", error.message);
}

Summary of Key Points

  • Dynamic Alerts: xrm-ex alerts automatically adjust height and width based on content, solving OOTB limitations.
  • Field Notifications: Highlight errors directly on fields with .addNotification() and remove them with .removeNotification().
  • Form Notifications: Provide broader context across the form using .addFormNotification() and .removeFormNotification().
  • Global Notifications: Display app-wide messages with .addGlobalNotification() and remove them dynamically.
  • Dynamic Logic: Combine alerts and notifications to create responsive forms and better user interactions

Conclusion

The xrm-ex library significantly improves Dynamics 365 notifications and alerts, solving key limitations like fixed alert sizes in the OOTB system. Whether you need form-specific messages, field-level highlights, or global notifications, xrm-ex provides an efficient and intuitive API for dynamic and user-friendly forms.


Comments