Identify “Auto Save” in JScript onload & onsave events – CRM 2013
CRM 2013 has got a new ‘Auto Save” feature which periodically saves the form after new data entered.
Problem with frequent auto save
- The problem with this design is whenever auto-save fires, it causes unwanted execution of the registered onload & onsave events.
To identify ‘Auto Save’ event in ‘onload & onsave’ below is useful approach
On Save
- Check the ‘Save Mode’ of ‘Event Args’, If Save Mode is ‘70’ then save event is caused by ‘Auto Save’
- Make sure to check ‘Pass execution context as first parameter’ while registering ‘onsave’ event
function onsave(eventArgs) {
var saveMode = eventArgs.getEventArgs().getSaveMode();
// 70 – AutoSave
if (saveMode == 70) {
alert(“Save caused by AutoSave”);
// By setting Form dirty, we will make sure the traditional save event executes when you click on ‘Save’ button
Xrm.Page.data.setFormDirty(true);
}
else {
alert(“Traditional Save event”);
}
}
Note –
- Since the ‘AutoSave’ periodically saves the form data, you might not get traditional ‘onsave’ event fired.
- If you want your logic to be executed in traditional ‘onsave’ event, set form dirty to ‘true’ in ‘AutoSave’ save mode.
On Load
‘onload’ event caused by ‘AutoSave’ does not clears the global variables. So use below steps to check ‘AutoSave’ event on ‘onload’
- Declare and set a global variable
- Check the variable on onload event
var formLoaded = false;
function onload() {
if (!formLoaded) {
alert(“Traditional form load happend !!!”);
}
formLoaded = true;
}
- Above function the ‘If’ block executes only first time when you open the form
🙂
-
January 6, 2014 at 2:11 PMDuplicate detection functionality in CRM 2013 | Rajeev Pentyala - Dynamics CRM Blog