Onload event does not fire when a record is saved in Dynamics 365/ CRM. What’s the workaround?

Follow my blog for more interesting topics on Dynamics 365, Portals and Power Platform. For training and consulting, write to us at info@xrmforyou.com
Recently I was working with a customer and came across a requirement where they needed to fire an event on client side every time once the data is saved successfully to process some post-save operations.
As we all know from version CRM 2013 and onwards the formload event does not fire when save is completed. So what is the other way around?
Well there is way to do that. We need to use the formContext.data.addOnLoad to add an event which fires every time when the data is refreshed.
The below code adds the onLoad event of data refresh in the form load event of the account entity.

function formLoad(e) {
    var formContext = e.getFormContext();
    formContext.data.addOnLoad(dataOnLoad);
    console.log("form load called.");
}
function dataOnLoad(e) {
    // write your code here.
    console.log("data onload fired");
}

formLoad is the event which is registered through Form events. In the formload we are adding the event for data refresh using the formContext.data.addOnLoad method.
The function dataOnLaod fires on the initial page load as well as every time the user clicks on save. And now you have a way to perform your post-save operations in client side whenever the record is saved.
Also please note that the function also fires if formContext.data.refresh() function is called.
Debajit Dutta
(Dynamics MVP)
For consultation/ training visit
http://www.xrmforyou.com or reach out to us at info@xrmforyou.com