Thursday, May 23, 2013

Capturing Save Event Arguments using Execution Context Parameter in CRM2011


When working with Dynamics CRM form scripting, there are several occasions where one needs to know the execution event for performing manipulation through scripting. For this reason Dynamics CRM provides execution context as an optional parameter that can be passed to a JScript library function through an event handler.
In below screenshot you can see “Pass execution context as first parameter” check box checked which passes execution context to the script function in CRM form.



There are different ways an entity record gets saved in Microsoft Dynamics CRM. This can be determined by getEventArgs() in execution context parameter as below:

function Form_onsave(executionObj) // exectuionObj passed as First Paramter
{
var eventmode=executionObj.getEventArgs().getSaveMode();

if (eventmode==2) { // returns 2 for Save and Close.
///
// Do the necessary logic
///
    }
}

The above code is an example to check “Save and Close” to perform some action. Similarly we can detect different save by return value from getSaveMode(). Below is the list of different save modes.

Entity
Event Mode
Value
All
Save
1
All
Save and Close
2
All
Save and New
59
Activities
Save as Completed
58
All
Deactivate
5
All
Reactivate
6
User or Team owned entities
Assign
47
Email (E-mail)
Send
7
Lead
Qualify
16
Lead
Disqualify
15

 Hope this was helpful!

No comments:

Post a Comment