July 16, 2009

Show custom notifications on Microsoft Dynamics CRM - upgrade

Marco Amoedo created cool javascript function that displays custom warning messages in Microsoft Dynamics CRM 4.0. http://marcoamoedo.com/blog/show-custom-warning-messages-on-microsoft-dynamics-crm/

I liked the idea so much, that I took the script and upgraded pimped it.

What it does?

The function simply adds a notification on top of the form, like in the picture below.

image

You can add multiple notifications with different icons.

image

Update or delete previously added notifications or add new ones. This image shows (after Name onChange event) changed error text, updated third notification to info style and a new warning. Compare it with previous picture.

image

How to use it?

Step 1: Insert this code into form OnLoad event.

/*============== addNotification function =============






Adds a message on the top of the entity form using



the same visual style as Microsoft CRM.



If function is called more than once,



the messages are added at the bottom of the list



If the message with same ID already exists,



then the message is updated and not added to a list.



It also supports deleting of previously added notifications.






Put this function in the OnLoad event,



so you can use it in all form and field events.






Parameters:



message - message to be shown to the user



messageType - Type of the message: 1 - critical, 2 - info, 3 - warning



notificationId - ID of the notification (needed for updating purposes)



    



Created by:



Marco Amoedo (http://marcoamoedo.com) - the idea and initial function



Dejan Dular (http://a-crm.blogspot.com) - upgraded functionality (different icons, updating and deleting notifications)



    



=======================================================*/



 



addNotification = function(message, messageType, notificationId)



{



    var notificationsArea = document.getElementById('Notifications');



    



    if (notificationsArea == null)



    {



        //Don't display message when deleting a notification.



        if (messageType != 4)



        {



            //Sorry. Notifications are not possible on this form.



            //Display a message box instead of notification.



            alert(message);



        }



        return;



    }



    



    var notificationDIV = document.getElementById('NotificationDiv_' + notificationId);



    



    //Delete the notification



    if (messageType == 4)



    {



        if (notificationDIV != null)



        {



            //Remove the notification



            notificationsArea.removeChild(notificationDIV);



        }



        return;



    }



    



    //Get the notification image. The default is info image.



    var notificationImage;



    switch (messageType)



    {



        case 1:



            notificationImage = '/_imgs/error/notif_icn_crit16.png';



            break;



        case 2:



            notificationImage = '/_imgs/error/notif_icn_info16.png';



            break;    



        case 3:



            notificationImage = '/_imgs/error/notif_icn_warn16.png';



            break;    



        default:



            notificationImage = '/_imgs/error/notif_icn_info16.png';



    }



 



    //Create notification



    var notificationTable = '<TABLE cellSpacing="0" cellPadding="0"><TBODY><TR><TD vAlign="top"><IMG class="ms-crm-Lookup-Item" alt="" src="' + notificationImage + '" /></TD><TD><SPAN>' + message +'</SPAN></TD></TR></TBODY></TABLE>';



    



    //Check if the notification with same ID already exists



    if (notificationDIV == null)



    {    



        //Create a new notification



        var notificationHTML = '<DIV class="Notification" ID="NotificationDiv_' + notificationId + '">' + notificationTable + '</DIV>';



        notificationsArea.innerHTML += notificationHTML;



        notificationsArea.style.display = 'block';



    }



    else



    {



        //Update the notification.



        notificationDIV.innerHTML = notificationTable;



    }



}



/*============= END addNotification function ===========*/




 



Step 2: Run the following function when there is a need for a notification.




addNotification(string message, int messageType, string notificationId)







Parameters:




  • message – Notification message displayed


  • messageType – Type of the notification (the icon) (1 – critical, 2 – info, 3 – warning, 4 – delete the notification)


  • notificationId – Any string that identifies specific notification. If a function is called later with existing notificationId, the notification will be updated. Otherwise a new notification is added at the bottom of the list. If you want to delete a specific notification, just set messageType = 4 and use correct notificationId.



Thanks again to Marco Amoedo.



 



Originally published by Dejan Dular