Wednesday, 2 March 2016

How to get All Dirty Fields in a Form Using Java Script In MS CRM 2011

Today i got a requirement where i need to get all fields which have been modified by the user.
Also there is additional requirement to set their submit-mode to Never.

Sharing you XRM Script for the same.

function MakeFieldUnDirty() {
    var attributes = Xrm.Page.data.entity.attributes.get();
    for (var i in attributes) {
        var attribute = attributes[i];
        if (attribute.getIsDirty())
            attribute.setSubmitMode("never")
    }
}

------------------------------------------------

Making fields read-only is a great way to restrict change or to make sure input is valid. However, all read-only fields are not submitted by default onSave event.
This is not desirable if you are automatically populating fields from user input on another field(s) and those fields cannot be changed by user. Those auto-populated fields will never be saved.
To make sure the fields are saved, we can call javascript function setSubmitMode.
Xrm.Page.getAttribute("<field name>").setSubmitMode("<submit value>");
Submit values available are:
  • always
  • never
  • dirty