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:
- Display important messages to users.
- Alert users about missing or incomplete information.
- Warn users before they perform critical actions.
- Improve data quality and ensure compliance.
- Provide guidance without the need for custom dialog boxes.
Prerequisites
Before you begin, ensure you have the following:
- Access to Dynamics 365 CRM customization settings.
- Permissions to create and manage Web Resources.
- A solution where your customizations can be added.
- A foundational understanding of JavaScript.
Step 1: Create a JavaScript Web Resource
Navigate to the customization area in Dynamics 365:
- Go to Advanced Settings.
- Select Customizations.
- Click on Customize the System.
- 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:
- message: The notification text to be displayed to the user.
- level: The type of notification. This can be
INFO,WARNING, orERROR. - uniqueId: A unique identifier for the notification, which is essential for managing or clearing it later.
Step 2: Add the Web Resource to the Form
Open the entity form where you want to implement the notification:
- Click on Form Properties.
- Add the JavaScript Web Resource you just created (
new_FormNotifications.js). - Under the Form Libraries section, select the uploaded script.
- Register the function
showFormNotification. - Select the OnLoad event for the form.
- 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:
- Save the form.
- 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:
- Use clear and meaningful messages.
- Avoid displaying too many notifications simultaneously, as this can overwhelm users.
- Reserve
ERRORnotifications for situations that genuinely prevent progress or data integrity issues. - Ensure notifications are cleared promptly after the associated validation succeeds or the condition is resolved.
- Keep messages concise, user-friendly, and actionable.
- Always use unique notification IDs to ensure precise control over individual notifications.
Benefits of Form Notifications
Implementing form notifications provides several advantages:
- Better User Experience: Users receive immediate feedback and guidance.
- Improved Data Quality: Real-time validation helps prevent incorrect data entry.
- Real-time Validation Feedback: Users are alerted to issues as they occur.
- Reduced User Errors: Clear guidance minimizes mistakes.
- Easy Implementation with JavaScript: Requires minimal coding effort for significant impact.
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.