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

20 thoughts on “CRM 2013 New Features: JavaScript Notifications

  1. Pingback: CRM 2013 – Client API New Functionalities Recap | Salim Adamon - Dynamics CRM Blog

  2. Pingback: CRM 2013 – Client API New Functionalities Recap - Salim Adamon - Dynamics CRM Blog - Microsoft Dynamics CRM - Microsoft Dynamics Community

  3. Pingback: CRM 2013 – Client API New Functionalities Recap | Jwalin Khatri

  4. Pingback: CRM 2013 : SDK New Features – Client API | CRM FORTRESS

  5. Pingback: CRM 2013 SDK New Features – Client API | CRM Rockstars

  6. Pingback: Notifications | Bite Size CRM

  7. Pingback: Hosk’s Top CRM 2013 articles of the week 16th May 2014 | Hosk's Dynamic CRM Blog

  8. Pingback: Hosk’s Top CRM 2013 articles of the week 16th May 2014 - Hosk's Dynamic CRM 2011 Blog - Microsoft Dynamics CRM - Microsoft Dynamics Community

  9. Pingback: Hosk’s Top CRM 2013 articles of the week 16th May 2014 | Dot Net RSS

  10. silviaguillen90

    Hello

    Im using setFormNotification on CRM2013 on InternetExplorer works fine but in chrome notifications with level Warning or Error fail.

    Can help me?

    Thanks &Regards

    Reply
  11. Pingback: Hosk’s Top CRM 2013 articles of the week 30th May 2014 | Hosk's Dynamic CRM Blog

  12. Pingback: Hosk’s Top CRM 2013 articles of the week 30th May 2014 - Hosk's Dynamic CRM 2011 Blog - Microsoft Dynamics CRM - Microsoft Dynamics Community

  13. Mark Pitts

    It appears, notifications do not work on forms that did not get upgrade, like quote detail.

    I tried setformnotification and got some weird message, ‘method not supported ‘trim’.
    I tried setnotification and did not see the message appear next to the field.

    Can anyone confirm this?

    Cheers,
    Mark

    Reply
    1. Mark Pitts

      I figured out why notifications on quote detail was not rendering. When I was calling

      Xrm.Page.ui.setFormNotification(message, ‘WARNING’, notificationId);

      I was passing notificationId was a numeric. Changing this to a string resolved the errors of my ways.

      Reply
  14. Pingback: Hosk’s Top CRM Articles of the Year 2014 | Hosk's Dynamic CRM Blog

  15. Pingback: Hosk’s Top CRM Articles of the Year 2014 - Microsoft Dynamics CRM Community

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s