Some time ago I needed to find all dirty attributes on form. Easy I thought. It occurs that some at some entitites getIsDirty() flag for field statuscode is always set – check on clean form load. To get all changed fields I’ve written method that read all dirty fields on form excluding specified fields.
GetDirtyFields: function() {
/// <summary>
/// Returns array of dirty attributes info.
/// </summary>
var excludedFields = arguments;
function IsFieldExcluded(fieldName) {
for (var i = 0; i < excludedFields.length; i++) {
if (excludedFields[i] == fieldName)
return true;
}
return false;
};
var attributes = Xrm.Page.getAttribute();
var dirtyAttributes = new Array();
attributes.forEach(function(formAttribute, index) {
var attributeName = formAttribute.getName();
if (formAttribute.getIsDirty() && !IsFieldExcluded(attributeName)) {
debugger;
var attributeTextValue = formAttribute.getValue().toString();
var attributeLabel = Xrm.Page.ui.controls.get(attributeName).getLabel();
dirtyAttributes.push({
Name: attributeName,
TextValue: attributeTextValue,
Label: attributeLabel,
Index: index
});
}
});
To be sure all fields has its ‘dirty’ flag please do read this.
