"тнιѕ вℓσg ¢συℓ∂ ѕανє уσυя мσηєу ιƒ тιмє = мσηєу" - ∂.мαηנαℓу

Sunday 6 October 2013

Autosave feature in CRM 2013 OR How to prevent Autosave feature in CRM 2013 using javascript OR How to identify Autosave in CRM 2013 using javascript

Version used for this post : CRM 2013 online trial.

Autosave is a cool feature in CRM 2013 which is based on a 30 seconds timer. In simple words, the form saves every 30 sec. But we should be a bit careful with this feature, just because there might be plugins running on Update OR javascript running on SAVE events.

Firstly lets see how auto save works. So on any record you could find a little save button on the right bottom corner of the form.


 If we make any changes to the existing record, we could see it turns to

And after 30 sec, we could see that CRM saves the change that we made.



1. The organizational level setting for autosave could be found in

Settings -- > Administation -- > System settings --> General


2.  We may not prefer to set as 'No' in the organization level as the autosave will be disabled for all entities. 

CRM 2013 SDK talks about a js method  "This method is essential if you want to enable auto-save for most forms in an organization but disable it for specific forms" ( Ref: CRM 2013 SDK)

execObj.getEventArgs().getSaveMode()

So this method could prevent Autosave.

The next Q is 

How do we identify whether its Autosave or user clicked on the bottom right corner  little SAVE button.

Again SDK is our guide here.

Ref: CRM 2013 SDK
"This method is essential if you want to enable auto-save for most forms in an organization but disable it for specific forms. The following code registered for the onSave event with the execution context passed to it will prevent any saves that initiate from an auto-save but allow all others. With auto-save enabled, navigating away is equivalent to Save and Close. This code will prevent any saves that are initiated by the 30 second timer or when people navigate away from a form with unsaved data."
function preventAutoSave(econtext) {
    var eventArgs = econtext.getEventArgs();
    if (eventArgs.getSaveMode() == 70 || eventArgs.getSaveMode() == 2) {
        eventArgs.preventDefault();
    }
}

Now its time for us to try this code snippet.

3. Lets see how could we apply this for the account entity. So lets navigate to solution and open the account entity form as shown below.


4. Open the account form and click on Form properties.


5. First we need to add a library. Click on Add.



6. Web resource library could be defined as follows. And then click on Text editor.



7. Lets add the code snippet from SDK.



8. And we need to SAVE and PUBLISH this webresource js library. ( Please note that its possible to add this js webresource library by directly navigating to Solution--> Webresource--> Add a new webresource. )


9. Now we need to choose the webresource library that we created and click on Add.


10.  Please note that we selected the webresource library and choose the event as OnSave. And then click on Add function which is located below the Form library.




11. Lets define the function as follows. Please note that we need to tick the Pass execution context as first parameter.Lets say okay and press okay button.



12.  Now lets SAVE the form and PUBLISH form.

13 Lets see how to test this. Just change something on the account record and we could see that the save button changes to Unsaved changes. But even after 30 sec or more time, it doesn't save anything automatically.

14. The change would get saved only when the user clicks on the save button. So we disabled the autosave feature only for Account entity with our friend CRM 2013 SDK.

15. Now consider another scenario. We do not want to disable the autosave but we would like to execute some js code when the record is not autosaved. Lets call the function as RecognizeSave.

function RecognizeSave(econtext) {
    var eventArgs = econtext.getEventArgs();
    if (eventArgs.getSaveMode() == 70 || eventArgs.getSaveMode() == 2) {
      
// The js code which should be executed during auto save goes here.
//Please note we are not preventing autsave in this case.
    }
else
{
// The  js code that should not be executed during the auto save mode goes here.
}
}

No comments:

Post a Comment