Friday, January 20, 2017

How to prevent a record being saved in CRM

In CRM, we can prevent the save of a record using either a plugin, synchronous workflow or a javascript.

Using a Plugin you can do by throwing an InvalidPluginExecutionException. This would stop the record from being saved. This can be done by a pre plugin.

With the introduction of CRM Synchronous workflows, you can achieve the same result. There is an option within the workflow to say stop workflow with status of Canceled. Here you can give an error message also. The only issue with this would be, you wouldn’t be able to do complex checking here without a custom workflow being written. But this is a very nice feature as you can quickly come up with a workflow for a simple validation without having to write code.
The next is Javascript. You can use this to do complex validations if you know your javascript well. Then you can stop the page from saving. You need select the save event with passing the parameter “Pass execution context as first parameter” for this.

Here, one need to be careful using javascript if you are going to check for the event to stop from saving. As there are events in CRM javascripts you need to choose for which save mode you want to stop this from happening. This MSDN article describes about save mode very nicely.

If you use a code like below it would be very good to use mode 70 which is the auto save mode as is the auto save of the form is enabled if that line is not there the record would be saved. That would fail the requirement. It would be better that you always use that mode if you really want to stop the record from saving.

function preventSave(context) {
    if (context != null && context.getEventArgs() != null) {
        var eventArgs = context.getEventArgs();
        if (eventArgs.getSaveMode() == 1 || eventArgs.getSaveMode() == 70) {


No comments:

Post a Comment

Retrieving Calendar of a Bookable Resource in Dynamics

There are occasions where we need to retrieve working days and working times of a resource in Dynamics grammatically. This is quite possible...