Dynamics 365 – Different Ways of Showing Form Level Error Messages – setFormNotification | setIsValid

Form notifications are useful when you want to prevent the user saving the form if the form fields do not meet the conditions.

Say you have 2 date fields called “Start Date” and “End Date”. You want the records can only be saved if the “End Date” is bigger than the “Start Date”.

You have 2 different options to show a form notification here:

You can use setFormNotification() function when validation fails and use clearFormNotification() when validation pass. You need to specify message, level and uniqueId parameters.

Or you can use setIsValid() function. You call it only once and pass it validation result with error message. It is definitely less code lines 😎

EndDateValidationWithFormNotification: function (executionContext) {
var formContext = executionContext.getFormContext();
var startDate = formContext.getAttribute("my_startdate").getValue();
var endDate = formContext.getAttribute("my_enddate").getValue();
if(endDate > startDate)
formContext.ui.clearFormNotification("EndDateValidation");
else{
formContext.ui.setFormNotification("End Date must be bigger than Start Date", "ERROR", "EndDateValidation");
formContext.getAttribute("my_enddate").setValue(null);
}
},
EndDateValidationWithIsValid: function (executionContext) {
var formContext = executionContext.getFormContext();
var startDate = formContext.getAttribute("my_startdate").getValue();
var endDate = formContext.getAttribute("my_enddate").getValue();
var valid = endDate > startDate;
formContext.getAttribute("my_enddate").setIsValid(valid, "End Date must be bigger than Start Date");
formContext.getAttribute("my_enddate").setValue(null);
},
Advertisement

One thought on “Dynamics 365 – Different Ways of Showing Form Level Error Messages – setFormNotification | setIsValid

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 )

Facebook photo

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

Connecting to %s