Know Details about new Async OnSave Events PostSave (Async OnSave Event) in Dynamics 365 CRM / Dataverse (PowerApps)

Sending
User Review
0 (0 votes)

Introduction

I am sure you may have used OnSave event several times in your projects. We generally use this event and write code in it to do some validation on the record to restrict the user from saving the record (using prevenDefaults method).

The nature of OnSave event is synchronous. It is not the right place to call asynchronous code because it does wait for it and we may see unsaved changes. If we make a synchronous network request in OnSave event, it can cause a slow experience and an unresponsive page.

Microsoft has introduced new aysnc onsave (PostSave) events. This event is called after save event is complete and it support after Save actions when the save event is successful or failed.

We will see how this async event is useful over save event. On load of Account form, I have added two events like below.

FormLibrary.prototype.onLoad = function (execContext, attributes) {
/// <summary>
/// This function will load crm web api on load of form
/// </summary>
var functionName = “onLoad”;
try {

var formContext = execContext.getFormContext();

formContext.data.entity.addOnSave(Account_FormLibrary.SaveFunction);
formContext.data.entity.addOnPostSave(Account_FormLibrary.PostSaveFunction);

}
catch (ex) {
console.log(ex.message);
}};

Post Save Event function 

FormLibrary.prototype.PostSaveFunction = function (executionContext) {
var formContext = executionContext.getFormContext();
var RecordId = formContext.data.entity.getId();
var fetchXml = “<fetch version=’1.0′ output-format=’xml – platform’ mapping=’logical’ distinct=’false’>” +
“<entity name=’account’>” +
“<attribute name=’name’ />” +
“<filter type=’and’>” +
“<condition attribute=’accountid’ operator=’eq’ value=’” + RecordId + “‘ />” +
“</filter>” +
“</entity>” +
“</fetch>”;
queryString = {
FetchXml: fetchXml,
FormattedValues: true
};
var accountCollction = Account_FormLibrary._crmAPI.CustomFetch(“accounts”, queryString, null);
};

Save Event function

FormLibrary.prototype.SaveFunction = function (executionContext) {
var formContext = executionContext.getFormContext();
var RecordId = formContext.data.entity.getId();
};

Now when you click on OOB Save button, save event will trigger, and below will be the result.

async OnSave events PostSave

Here the record ID is coming blank because save event is not completely done. So I am not able to find related contacts. But when I check the Post save event result I am able to get record ID because it is calling once the save event completely done.

async OnSave events PostSave

Here are some client API Microsoft added which can be useful while save or post save event –

executionContext.getEventArgs().getEntityReference();

If save succeeds, it returns entity ID, and entity name.

async OnSave events PostSave

executionContext.getEventArgs().getIsSaveSuccess();

You can use this client API to check whether save operation is successful or failed.

async OnSave events PostSave

executionContext.getEventArgs().getSaveErrorInfo();

This will provide the error info on save event. If we have a plugin or action on create/update of record and if it is failing then this method shows the error information thrown from the plugin or action.

async OnSave events PostSave

Conclusion

You can use this information to use the new Async OnSave Events PostSave (Async OnSave Event) in Dynamics 365 CRM / Dataverse (PowerApps).