prevent save event of a record in Dynamics 365 forms with custom message – use the setIsValid method

We all know that in UCI, when we try to save a record and if one or more of the form fields are does not contain valid data, the system stops the record from being saved and appropriate validation notifications are shown on the top of the form.

For example I am on the contact form and I haven’t entered the last name which is mandatory and entered an email address in incorrect format.

When I try to save the record, I get validation notifications on top of the form as you can see from the screenshot.

image

Well this is good. But the problem with this is, the system will show this only if you leave some mandatory field blank or you enter data in incorrect format which is not supported by the field by design. What if you have some custom validation logic and based on that you want to show a validation notification for the attribute in the same format and style?

You can use business rules but that would show notification beside the field and not on the top of the form. Also complex validation logic cannot be achieved by using business rules. Also form notifications wont stop save from happening. So how can we achieve this?

Well this is quite easy. There is infact a method in the API which does the same. The method name is setIsValid()

formContext.getAttribute(“lastname”).setIsValid(false, “Validation failed for lastname field.”);

Now when you try to save the record, you get the field validation notification just where you need

image

And your save event is stopped as well. No fancy code to stop the save event.

Hope this helps!

Debajit Dutta

(Dynamics MVP)

For consultation/ corporate training visit www.xrmforyou.com or reach out to us at info@xrmforyou.com

Our product offerings:

CRM-Sharepoint Attachment uploader and metadata manager (http://www.xrmforyou.com/sharepoint-integrator.html)

Notes Manager (https://debajmecrm.com/add-metadata-to-your-notes-and-attachments-in-dynamics-notes-metadata-manager-from-xrmforyou-com/)

Role based views for Dynamics 365 (http://www.xrmforyou.com/role-based-views.html)

Record Cloner for Dynamics 365 (http://www.xrmforyou.com/record-cloner.html)


Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe to get the latest posts sent to your email.

Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading