CRM 2013: JavaScript – Disabling Form AutoSave

Nathan Eccles, 23 October 2013

CRM 2013 brings with it the system-wide introduction of process forms, and alongside that, auto-saving on all entities by default. This nifty new feature makes for a smoother over-all user experience, and a flatter feel to the system. However it is quite a bit different from the CRM 2011 ways of doing things, and may not be welcome in all systems.

Microsoft anticipated this though and gave us the option in System Settings to disable it throughout the entire system. This option is also queryable from outside the system for use in custom applications, and is automatically applied to the Mobile Client Application (MoCA).

But what if we only want to disable it for specific entities within our CRM system? Microsoft thought of this to, and while it’s not quite as simple as changing a setting, a small amount of javascript will do the trick!

Simply apply the below javascript to the OnSave event of the entity you wish to disable auto-saving for and tick the “Pass execution context as first parameter” box in the Handler Properties.

function stopAutoSave(context) {
    var saveEvent = context.getEventArgs();
    if (saveEvent.getSaveMode() == 70) { //Form AutoSave Event
        saveEvent.preventDefault(); //Stops the Save Event
    }
}

If you don’t want the other form OnSave events to run when the AutoSave event is cancelled, simply make sure you “stopAuoSave” function is the first OnSave event that runs, and add the following code to the top of each additional OnSave event (ensuring that you have ticked the “Pass execution context as first parameter” box for each event).

if (context.getEventArgs().isDefaultPrevented()) { return; }

This means we can make the most of the AutoSave feature throughout the majority of the system, but disable it in the places where it interferes with current business processes.