Dynamics CRM: Notification for Client API Scripting

We have two types of notification which are useful when we want to populate any message to a user using client-side scripting.

  1. Form Notification
    • Syntax: formContext.ui.setFormNotification(message, level, uniqueid);
  2. Field Notification
    • Syntax: formContext.getControl(“logicalname”).setNotification(message, uniqueid);
  1. Form Notification is used when we want to populate any message to users on the form level. This is not specific to any attribute. It does not stop the user from saving the form,
    • Syntax: formContext.ui.setFormNotification(message, level, uniqueid);
    • message –> Here we need to write the message/alert which would get populated to the user.
    • level –> we have three types of levels. This is used to display an appropriate icon in front of the message.
      • INFO
      • ERROR
      • WARNING
    • uniqueid –> This can be any user define string value.

Field Notification is used when we want to display any error message to a user and restrict the user from saving the form until the error is cleared up.

We will learn how to validate the phone number format entered by the users.
I will use the namespace pattern for writing the Javascript. I have already explained the namespace function in one of my previous blogs. Click here to visit the blog.

Now, let’s continue with the contact entity where we would validate the phone number format for the mobile phone attribute.
Below is the code which can be used for validating the phone number format and notify the user when the entered phone number does not meet the required format criteria.

var Namespace = window.Namespace || {};
(
function (){
this.formOnLoad = function(executionContext)
{
}
this.validatephonenumber = function(executionContext)
{
var formContext = executionContext.getFormContext();
var phoneNumber = formContext.getAttribute("mobilephone").getValue();

//Using Regular expression
var regexpr = /(((\d{3}) ?)|(\d{3}-))?\d{3}-\d{4}/;

//Uing test for a match in a string. Returns true or false
if(!regexpr.test(phoneNumber))
{
/*Using field notification for displaying the error msg. User won't be able to save the form until the error is cleared up.*/
formContext.getControl("mobilephone").setNotification("Entered Phone number is not in a correct format", "validatephn");

//Using form notification for displaying the msg on the Form.
formContext.ui.setFormNotification("Warning Message", "WARNING", "formwarning");
}
else
{
/*Clear error notfication when the entered phone number is in the correct format. For clearing the notification only uniqueid is required.*/
formContext.getControl("mobilephone").clearNotification("validatephn");

/*Clearing form notification when the criteria is met. For clearing the notification only uniqueid is required.*/
formContext.ui.clearFormNotification("formwarning");
}
}
}
).call(Namespace);

Below is the screenshots of field/form notification. I have called the script on Contact entity and it would trigger on mobile phone attribute On Change event.

We can see that once the error(phone number format) is resolved the notification has been automatically cleared up.

Use the link to know more about Regular Expression.

I hope this blog helps in understanding the notifications, their types and when to use form or field notification.

One Reply to “”

Comments are closed.

Design a site like this with WordPress.com
Get started