Dynamics CRM Model-Driven-Apps: formContext.data.entity.addOnPostSave vs modified_OnChanged

What is the way that you used Dynamics CRM to call the javascript method after saving the entity? For example, after the record is saved, you want to re-run the form_onLoad function again 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 () {
        }, 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) {
    this.modified_onChanged = function (context) {
        setNotification(context, "FROM: modified_onChanged", "_modified_onChanged");

And when I saved the form, here is the result:

After the data is saved, the function triggering the Form Notification

addOnPostSave Method

The first method is formContext.data.entity.addOnPostSaveThe formContext.data.entity.addOnPostSave is a method that lets the developer register a custom function to be executed after the data is saved (success/fail). 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:

  1. Add Modified On attribute into the form (You can set Visible to false).
  2. Add new Event > register your modified_onChanged javascript function to this attribute (Modified On).

The below picture is how I register the function:

Registering javascript function in Modified On Change

If you wondering why is it working. The answer is CRM will be comparing the value of 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 (the 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

The system will replace all old attribute values, hence the event will be triggered


  • 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 call 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.

2 thoughts on “Dynamics CRM Model-Driven-Apps: formContext.data.entity.addOnPostSave vs modified_OnChanged

  1. Does addOnPostSave method works on Appointment form in a model driven app? For me it worked in all entities but Appointment.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.