Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Javascript to show FormNotification

(0) ShareShare
ReportReport
Posted on by

This Javascript snippet on the Street field displays the yellow message the top:

function NotificationTop (msg,type) {
Xrm.Page.ui.setFormNotification(msg, type);
}

Screen-Shot-2016_2D00_01_2D00_23-at-11.46.13-AM.png

I love the customer experience of this message and once it has been added as a Library via Web Resource, it can be reused on many fields by adding it as a Form Library on your form, then implementing it on a field by:

    1. Clicking the Edit button

Screen-Shot-2016_2D00_01_2D00_23-at-11.55.47-AM.png

  1. Then in the Parameters box, put your desired message text in quotes, then either ERROR, WARNING, OR INFORMATION as the message type.  This last part determines what icon appears; ERROR gets a red X, WARNING gets a exclamation mark, and INFORMATION doesn't show an icon.

Screen-Shot-2016_2D00_01_2D00_23-at-11.55.47-AM.png

Screen-Shot-2016_2D00_01_2D00_23-at-11.54.05-AM.png


With that all said, I would like to improve it to want it to behave similar to what happens when you try to advance in a Business Process Flow without providing the required fields.  It has two features:

Screen-Shot-2016_2D00_01_2D00_23-at-11.51.18-AM.png

  1. Clears after a # seconds
  2. Has a X so the user can clear it on demand
  3. Also ... would like it to not appear when the record is first created; in essence, that's not really "on change" but the system is displaying it anyway.  I would like it to only show after the record is saved and the user comes back in and changes the address.

How would that Javascript need to change to accommodate 1 thru 3 above?

*This post is locked for comments

  • Cyclefitness Profile Picture
    Cyclefitness on at
    RE: Javascript to show FormNotification

    Thanks, Guido.  That, together with putting the parameters in, did the trick!

  • Cyclefitness Profile Picture
    Cyclefitness on at
    RE: Javascript to show FormNotification

    BEAUTIFUL!!!!  I get so excited when things like this work.  ;-)

    Thank you so much!!

  • Suggested answer
    Borist Profile Picture
    Borist 180 on at
    RE: Javascript to show FormNotification

    I guess you are missing the function arguments.

    Try this code :

    function DisplayNotification(message,type,id) {

    var time = 3000; //Display time in milliseconds

    //Display the notification

    Xrm.Page.ui.setFormNotification(message, type, id);

    //Wait the designated time and then remove

    setTimeout(

    function () {

    Xrm.Page.ui.clearFormNotification(id);

    },

    time

    );

    }

    And this configuration on the field :

    jsNotification.png

    I tested it on CRM 2016 (works like a charm) but it should work on CRM 2013/2015.

    Best,

    Boris T

  • Cyclefitness Profile Picture
    Cyclefitness on at
    RE: Javascript to show FormNotification

    My onchange event looks like this:

    function DisplayNotification() {

       var time = 3000; //Display time in milliseconds

       //Display the notification

       Xrm.Page.ui.setFormNotification(message, type, id);

       //Wait the designated time and then remove

       setTimeout(

           function () {

               Xrm.Page.ui.clearFormNotification(id);

           },

           time

       );

    }

    And

     Handler.png

    BUT ... absolutely nothing happens on change.

  • Borist Profile Picture
    Borist 180 on at
    RE: Javascript to show FormNotification

    In fact, you must use the notification id if you want to use the clearNotification method.

    You may check the MSDN documentation : msdn.microsoft.com/.../gg327828.aspx

    In your case, you might add an argument (notificationId for instance), and you use it as described in the MSDN link.

    Best,

    Boris T.

  • Cyclefitness Profile Picture
    Cyclefitness on at
    RE: Javascript to show FormNotification

    Regarding "clears after X seconds", I modified the JS so it could receive parameters like this:

    function DisplayNotification() {

       var time = 3000; //Display time in milliseconds

       //Display the notification

       Xrm.Page.ui.setFormNotification(message, type);

       //Wait the designated time and then remove

       setTimeout(

           function () {

               Xrm.Page.ui.clearFormNotification();

           },

           time

       );

    }

    And I passed in two parameters on the Event Handler.:

    'Message','WARNING'

    I figured I could drop id since it's on the field it needs to function on.

    But nothing happens.  Rookie, I am.

    This is what it was before my modification:

    function DisplayNotification() {

       var message = "Useful message to the end user";

       var type = "INFO"; //INFO, WARNING, ERROR

       var id = "Info1"; //Notification Id

       var time = 3000; //Display time in milliseconds

       //Display the notification

       Xrm.Page.ui.setFormNotification(message, type, id);

       //Wait the designated time and then remove

       setTimeout(

           function () {

               Xrm.Page.ui.clearFormNotification(id);

           },

           time

       );

    }

  • Cyclefitness Profile Picture
    Cyclefitness on at
    RE: Javascript to show FormNotification

    Thank you, Boris.  I will study.

  • Suggested answer
    Borist Profile Picture
    Borist 180 on at
    RE: Javascript to show FormNotification

    Hello Deb,

    1. "Clears after # seconds" : you can use the Javascript function "setTimeout", combined with the clearNotification method. Check this blog article, with an example of what you want :

    https://community.dynamics.com/crm/b/jasonlattimersblog/archive/2014/04/28/simple-javascript-form-notification-with-timeout

    2. "Has a X to clear it on demand" : I don't think there is a supported way to do this. As a supported workaround, you may add a custom button in the command bar that clears notifications on the form.

    3. "Not appear on create" : you can use the function Xrm.Page.ui.getFormType() (documented on MSDN here : https://msdn.microsoft.com/en-us/library/gg327828.aspx#Anchor_5 ) to know if you are in creation or modification mode.

    Best,

    Boris

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,965 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,836 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans