Dynamics CRM: Canceling the Save operation

There are occasions where you need to abort the saving of a record within CRM. This is accomplished by using JavaScript to instruct CRM to prevent the default action ( which is a save operation ).

This requires a small piece of JavaScript in the OnSave Event which we will do in the following steps:

Adding a web resource for the contact form

In order to utilize JavaScript on our form, we must first attach a JavaScript library web resource to the form:



Next, we must add an OnSave event to the form:



Note: The pass execution context as first parameter checkbox is very important to this process. Without it, it will not work.


Inside the Contact_main_library.js web resource, we’ll add the following function:

function Form_onsave(executionObj)
    var shouldSave = true;

    if (shouldSave)
        alert("Unable to save because of some reason or the other.");


Inside the OnSave function, we need to have some type of test to see if we should allow the record to be saved.

For this demonstration, we simply check the value of a variable called shouldSave. If true, then we display an error message to the user then execute the preventDefault() method which will instruct CRM to cancel the save operation.

Note: in an actually CRM system you would probably perform checks such as validating data entered by the user is correct and/or appropriate.

It should also be noted that if you have required fields on the form, CRM will validate those fields first. If any required fields are blank, an warning message will be displayed to the user so they may complete those fields.

The OnSave event is only fired after all required fields have been populated.



Leave a Reply 5 comments