Saturday, December 17, 2011

CRM 2011 Diagnosing "Your Changes have not been saved" warning

Every one in a while, I will encounter a form where I open the form but don't change anything.  When I close the form, I get a pop-up that "Your changes have not been saved.  To stay on the page so that you can save you changes, click Cancel." (CRM 4)  In CRM 2011 you will get a CRM-styled Yes/No dialog instead.  Either way, you must address the pop-up to continue.  If you're getting a lot of these, your CRM experience will quickly degrade.

I have mainly seen this on forms which we've added customization to (JavaScript or IFRAMEs which reach in to the parent).  Luckily, it is fairly easy to diagnose using the IE Developer Tools.  Simply press F12 and go to the script tab.

Then paste in the following script to figure out the field in question:

CRM 2011:
// Get all attributes
var y = frames[0].Xrm.Page.getAttribute();
for(var x in y){
  var z = y[x];
  // If we can get is dirty on them, and they are dirty, print the name
  if (z.getIsDirty && z.getIsDirty()){
    console.log(z.getName());
  }
}

CRM 4:
// Not really an easy way to enumerate fields in CRM 4
for(var x in document.all){
  var y = document.all[x];
  // Check for existence of IsDirty method
  if (typeof(y.IsDirty) != "undefined" && y.IsDirty &&
      y.tagName != 'FORM')
    console.log(y.id);
}

And now we have our dirty field(s):

If you have any weird scenarios which cause a seemingly "faux" is dirty, post them here so we can keep a list.  So far I've found that:
  1. Entity reference fields in CRM 2011 wrongly report as being dirty if the committed data has untrimmed white-space OR has consecutive white-space.