Monday, April 28, 2014

Simple JavaScript Form Notification With Timeout

The form notifications in CRM 2013 are a pretty slick way to communicate some information to the end user without necessarily having to force the user to acknowledge the message. It in the past we’d use something like a simple JavaScript ‘alert’ to open up a pop-up message with the information. It worked well but it could be considered a bit of a distraction if the information was critical. Besides working on the browser and tablet clients, it gives developers a way to display some information without having to put it in the end user’s face. But the one downside is that in order to remove it from the screen you need to actually execute another piece of code to do so. To get around that, here is a quick example of using the JavaScript ‘setTimeout’ function to display a form notification and then remove it after a set period of time.

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

Here it is in action.

NotifcationTimeout