Tag Archives: Notifications

CRM 2013 New Features: JavaScript Notifications

CRM 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:

image

Now, if I make another update and then let another Save occur this is what I get:

image

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);

Here’s an example where I have added a SUBMIT FOR APPROVAL button to the Command bar on a CRM form and have added JavaScript validation to that button that generates field notifications if certain mandatory fields have not been populated.   Here’s the JavaScript:

function SubmitForApproval() {
    var AlertRequired = false;
    if (Xrm.Page.data.entity.attributes.get("new_unitamount").getValue() == null) {
        Xrm.Page.getControl("new_unitamount").setNotification("Unit Amount must be entered in order to submit for Approval");
        AlertRequired = true;
    }
    else {
        Xrm.Page.getControl('new_unitamount').clearNotification();
    }
    if (Xrm.Page.data.entity.attributes.get("new_qty").getValue() == null) {
        Xrm.Page.getControl("new_qty").setNotification("Qty must be entered in order to submit for Approval");
        AlertRequired = true;
    }
    else {
        Xrm.Page.getControl('new_qty').clearNotification();
    }
    if (Xrm.Page.data.entity.attributes.get("new_linetotal").getValue() == null) {
        Xrm.Page.getControl("new_linetotal").setNotification("Line Total must be entered in order to submit for Approval");
        AlertRequired = true;
    }
    else {
        Xrm.Page.getControl('new_linetotal').clearNotification();
    }
    if (AlertRequired == true) {
        alert('Missing information, please clear all alerts');
    }
    else {
        alert('Submitted!');
    }
}

And here’s how it looks when I test with missing data.  First the Alert fires…

image

And then each field that failed a non-null test is flagged with an Alert icon and the notification text is displayed:

image

This starts to look a bit ugly when you generate notifications for neighboring fields:

image

The notification text goes away once you mouse over the message, but the alert icon remains:

image

I wondered whether I could leave out the notification message and just get the alert icon to appear by just saying:  Xrm.Page.getControl("new_unitamount").setNotification();

Unfortunately, the notification speech bubble still appears so this is not a good approach:

image

I like the form notifications.  I would try to minimize usage so that no more than 3 form notifications ever appear, any more and they just aren’t visible enough. 

I’m not sure I like the field notifications.  I like the fact I can highlight fields with issues but I don’t like the notification speech bubbles that appear.  I guess it works well so long as notifications don’t appear for adjacent fields.  I would give this some consideration when deciding how to layout the fields.

Advertisements