Wednesday, 13 January 2016

Removing "unsaved changes" from MS dynamics online forms after saving



MS Dynamics CRM online

This topic is related to the dirty fields in MS dynamics CRM online forms, if user save any changes and after saving he would see the “Unsaved Changes” at the left bottom part of the page even if all the fields are saved, in my case I have used Javascript on both the load and save event also I have workflow activated behind every entity so the option in Administration area for Auto save is set to NO in my configuration. Because if we have workflows and JS behind the entity that is why we don’t allow Auto save functionality.
Now, it was real irritating for me to find the solution for this thing. At first stage, I tried to disable all the Javascript on my entity because I found this solution in most of the blogs and post. So I did this but that was useless for me. So, I found that there are some dirty fields in the entities which are causing the issue and due to these dirty fields, the form could not save and showing “Unsaved Changes” like below image


So, I have set the setSubmitMode to “never” in this case on page load Javascript. But the thing is that, if we set these fields as “never” submit mode than these fields won’t save into the database on saving event. And in my case, there are few fields which needs to be save. So what is observed during the task is that, if we set the submit mode to “never” on page load javascript and again just before saving through javascript if we again set the submit mode to “always” than it solve my issue because on page loading, MS dynamics page identify the dirty fields and if we set these fields to “never” than it won’t  affect the “unsaved changes” issue. I know this is trick but this is the only way I found the solution. So I have made a dynamic javascript which accept the submit mode and on load event, I set it to “never” and on saving it accept the “always” mode. See the below Javascript
function ClearDirtyFields(mode)
{
   var attributes = Xrm.Page.data.entity.attributes.get();
   for (var i=0;i<attributes.length ;i++)
   {
       if (attributes[i].getName() == "new_guid")
       {
          attributes[i].setSubmitMode(mode);
       }
               if (attributes[i].getName() == "custom_Membership")
       {
          attributes[i].setSubmitMode(mode);
       }
    }          
}

In above Javascript,, I explicitly set the field’s submit mode using for loop. Remember, set the “never” submit mode on page load javascript and “always” on save event javascript.