在CRM2011表单中取消OnSave操作

好像SDK里没有记这东西,在国外的一博客里找了个贴过来备忘。

 

转自:http://blogs.infinite-x.net/2011/05/06/crm-2011-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.");

        executionObj.getEventArgs().preventDefault();
    }
}

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.

原文地址:https://www.cnblogs.com/bcszz/p/2519593.html