In this blog, we will understand how to run JavaScript code after the record is saved in D365 CE(CRM) or Model-driven Apps.
To execute JavaScript code after the record is saved we need to use PostSave Event.
PostSave Event
- PostSave event occurs after the OnSave event is complete. This event is used to execute custom logic which needs to be performed after Save actions.
- We will use the addOnPostSave method to manage event handlers for this event.
Use Case: In my example, I create a project task record using the quick create form, and when a project task record is created open the newly created project task record.
Implementation
Syntax:
formContext.data.entity.addOnPostSave(myFunction)
PARAMETER:

Code:
var formCustomizations = {
runAftersave: function (executionContext) {
var formContext = executionContext.getFormContext();
formContext.data.entity.addOnPostSave(formCustomizations.openProjectTaskForm);
},
openProjectTaskForm: function (executionContext) {
var formContext = executionContext.getFormContext();
var taskId = formContext.data.entity.getId();
var entityFormOptions = {};
entityFormOptions["entityName"] = formContext.data.entity.getEntityName();
entityFormOptions["entityId"] = taskId.replace("{", "").replace("}", "");
entityFormOptions["openInNewWindow"] = true;
// Open the form.
Xrm.Navigation.openForm(entityFormOptions).then(
function (success) {
console.log("record opend sucessfully.");
},
function (error) {
console.log(error);
});
}
}
- Open the Project Task quick create form.

- Add the above code on the OnSave event of quick create form of Project Task.


Result:
I hope you find this helpful!!