What is the way that you used in Dynamics CRM to call the javascript method after saved the entity? For example, after the record is saved, you want to re-run again the form_onLoad function so you can see the latest state of your controls (set required/hide). Or probably you want to open WebResource or add Form Notification to the user. Here, I will be explaining about 2 methods that we can use to achieve this requirement.
Here is the code that we will learn today:
var Blog = Blog || {};
// This variable being used for make sure the event register only once per lifetime
var load;
(function () {
var setNotification = function (context, message, id) {
var formContext = context.getFormContext();
formContext.ui.setFormNotification(message, "INFO", id);
window.setTimeout(function () {
formContext.ui.clearFormNotification(id)
}, 10000);
};
var addOnPostSave = function (context) {
if (load) return;
var formContext = context.getFormContext();
formContext.data.entity.addOnPostSave(function () {
setNotification(context, "FROM: addOnPostSave", "_adddOnPostSave");
});
load = true;
};
this.form_onLoad = function (context) {
addOnPostSave(context);
};
this.modified_onChanged = function (context) {
setNotification(context, "FROM: modified_onChanged", "_modified_onChanged");
};
}).apply(Blog);
And when I saved the form, here is the result:

addOnPostSave Method
The first method is formContext.data.entity.addOnPostSave. The formContext.data.entity.addOnPostSave is a method that lets the developer register a custom function to be executed after the data successfully saved. So in the addOnPostSave method, you can see that we registered the custom function of setNotification. The important point here, you need to make sure the function only be registered once. That is why the variable load is being used.
Modified On Changed Method
The second method steps:
- Add Modified On attribute into the form (You can set Visible to false).
- Add new Event > register your modified_onChanged javascript function to this attribute (Modified On).
The below picture is how I register the function:

If you wondering why is it working. The answer is CRM will be comparing the value on each attribute from the backend, and compare it with the current front-end attribute’s value. If got changes to it, then the value will be replaced (Modified On attribute will always be updated, that is why we use this attribute to verify the changes). Hence the onChanged event will be triggered. This behavior is a long time not being changed and this is my way to go before I knew formContext.data.entity.addOnPostSave API.

Summary
- The formContext.data.entity.addOnPostSave provides a very easy-to-understand method to register custom functions after the saving process is successfully called.
- You need to make sure you only calling this formContext.data.entity.addOnPostSave once so the method is not registered so many times.
- The second method even though it is working, the method is more tedious because we need to add the Modified On attribute in the form + register on the change event.
Does addOnPostSave method works on Appointment form in a model driven app? For me it worked in all entities but Appointment.
LikeLike