Thursday, October 17, 2013

CRM 2013 NEW FEATURES: JAVASCRIPT NOTIFICATIONS

RM 2013 come with a set of new JavaScript functions that we can use for sending notifications to the user.  There are form notifications and field notifications.  Here’s an example of a form notification at work…
The function below was registered on the OnSave event of my form.   It notifies the user that a Save has occurred and timestamps the notification.  I post 3 notifications to demonstrate the 3 different types available (error, warning and information):
function NotifyOnSave() {
    var today = new Date();
    Xrm.Page.ui.setFormNotification('Error! A Save occured at ' + today, 'ERROR');
    Xrm.Page.ui.setFormNotification('Warning! A Save occured at ' + today, 'WARNING');
    Xrm.Page.ui.setFormNotification('Information: A Save occured at ' + today, 'INFORMATION');
}
Here’s what appears after the Save:

It’s a little hard to tell on the screenshot but what has happened is each of those 3 notifications has been posted a second time to the form (note: the UI only shows 3 rows of notification, the user has to scroll down to see the other 6 notifications).  The Information notification appears twice due to the sort order applied by the UI:  Information notifications first, then Error notifications followed by Warning notifications.   This is the default behavior  of setFormNotification().   We have options though, we can assign each notification a unique ID and then we can overwrite a previous notification and avoid duplication in a scenario like this.  Here’s the revised JavaScript (note the unique ID added as a 3rd parameter):
function NotifyOnSave() {
    var today = new Date();
    Xrm.Page.ui.setFormNotification('Error! A Save occured at ' + today,'ERROR','1');
    Xrm.Page.ui.setFormNotification('Warning! A Save occured at ' + today,'WARNING','2');
    Xrm.Page.ui.setFormNotification('Information: A Save occured at ' + today,'INFORMATION','3');
}

If you want to clear a notification you use clearFormNotification() and you identify the notification by its unique ID:
    Xrm.Page.ui.clearFormNotification('1'); 

We also get field notification in CRM 2013.  The syntax for these is:
Xrm.Page.getControl(fieldName).setNotification(message);