Dynamics 365 Server-side validation

Defining required and optional fields are always an important part of every Dynamics 365 project that I have ever worked on. Have you ever thought properly about the server-side validation on that matter? From my experience, Dynamics CRM Customizers/Developers usually just create fields with proper value in  “Field requirement” field. I hope that everyone is aware of the fact that this is only client-side validation, which can be easily changed from client-side code (JS) or Business Rules. This usually is enough, because CRM users were not aware of how this application works and would not run any malicious JavaScript on the form (wishful thinking…). Unfortunately, there are some great Chrome extensions, that allow to disable all the client-side validations in a single click and more and more users are aware of their existence, “hacking” the input for CRM for their convenience. Of course, more advanced CRM users were always aware of the fact that they can override the system behavior by using simple Import or some tools from XrmToolBox, so they even did not not have to know programming and CRM SDK. That’s why it’s sometimes really important to prepare some sort of Server-Side validation logic for really vital data, to avoid situation when a salesman does not fill Tax Number for a client, because he did not have time to search for the data before putting it inside CRM.

Maybe you already know that but there is a built-in functionality for Server-Side validation in CRM. These are simply Business Rules, which are set to “Entity” scope. Most of you know the Business Rules as something that allows CRM Power Users to perform some simplified tasks on the form without writing any JavaScript. Although I am still not a big fan of Business Rules as something that replaces JS, they come in very handy when it comes to this server-side validation. Just have a look at the following rule:

Configuration

If we register it for “Entity” scope we will not be able to save the record not only using standard forms:

Message

but also using SDK or Import:

Message2

What if we have so many entities and fields that creating and maintaining Business Rules for all of them is a nightmare? It’s quite easy to create a plugin that we can register on Pre-Validation and it will do all the dirty job for us. Have a look at this example:

You should register this plugin on Pre-Validation of Create/Update of any entity (for Update you should also register PreImage). It will retrieve the entity metadata and check all the required attributes. If any of this attributes is missing, it will fire an exception that will prevent saving of the bad data. This is just an example, the full code of the plugin can be found here:

https://github.com/pawelgradecki/Validation.Plugin

Authors:

Pawel Gradecki

Subscribe to oneDynamics

Sign up now to get access to the library of members-only issues.
Jamie Larson
Subscribe