Working with Notifications in CRM
Hello everyone,
In this article I am going to talk about how to work with notifications in CRM forms using javascript.
We can create 3 types of notifications with javascript:
- Information
- Warning
- Error
Using this notifications are more user-friendly to me than just using an alert function with prompting a message.
Showing notification on a CRM Form
The code below runs when the Contact Form loads. I showed the three types of notifications at the same time on the same Form. You can do it seperately if you would like to.
Xrm.Page.ui.setFormNotification('Hey! This is an ERROR notification', 'ERROR'); Xrm.Page.ui.setFormNotification('Hey! This is a WARNING notification', 'WARNING'); Xrm.Page.ui.setFormNotification('Hey! This is an INFORMATION notification', 'INFORMATION');
When the Contact form loads the result of the code above will be like that:
If you have done so far, you should be wondering how to hide these notifications on the Form. Do not worry. I will be explaining 🙂
Hiding notification on CRM Form
In order to hide a notification on a CRM Form we should have passed one more value to setFormNotification method to identify the notification.
For example if we have written our code above like below,
- The first notification would have had the the ID of 1
- The second notification would have had the the ID of 2
- The third notification would have had the the ID of 3
The reason of that we passed the ID values like 1, 2 and 3 respectively as third parameter to setFormNotification method .
Xrm.Page.ui.setFormNotification('Hey! This is an ERROR notification', 'ERROR', '1'); Xrm.Page.ui.setFormNotification('Hey! This is a WARNING notifcation', 'WARNING', '2'); Xrm.Page.ui.setFormNotification('Hey! This is an INFORMATION notification', 'INFORMATION', '3');
Now we can hide these notifications from the Contact Form.
To do that we need the code below:
Xrm.Page.ui.clearFormNotification('1'); Xrm.Page.ui.clearFormNotification('2'); Xrm.Page.ui.clearFormNotification('3');
Now that we have hidden the notification we show on the form before.
Notifications of CRM fields
We can also set notifications for CRM fields on the Forms depending on the condition of the corresponding field value.
To do that we write the below code:
Xrm.Page.getControl("CrmFieldName").setNotification("YourMessageAboutTheField");
The result of the code above for the address field will be like below:
Hiding notification of a CRM field
We can use the below code to hide a notification of a field.
Xrm.Page.getControl('CrmFieldName').clearNotification();
I hope you find this tutorial useful.
See you soon 🙂