Tracing the Dynamics CRM Form Data save Operation

I ran into an interesting issue this week where I was opening up a new Opportunity Entity form from the Account record via a Command Bar button.

The issue was that I was getting a warning that I was about to discard changes to the record I was leaving, and would l like to Continue or Cancel.

I verified all of the JavaScript and found that I was not performing any updates on form load, so there must be something else happening to modify a value on the form so that CRM thinks the record needs to be saved.

But what was being changed?

I thought about it for a bit and realized I could have CRM tell me what was happening, though a little known or used method called Xrm.Page.data.entity.getDataXml.

Dynamics CRM, by and large, does not send unaltered data to the database when a Save occurs. By using getDataXml, I can see exactly which field or fields were being modified.

Implementation

To implement this code, I created a small onSave method:

function onSave() {
    alert(Xrm.Page.data.entity.getDataXml());
}

Then created an OnSave event on the form properties:

image

Note: Make sure this event is the final OnSave event to ensure that all of the other OnSave events have completed.

This will display a message box showing what fields have been changed and the new values. Basically the same thing that will be sent to the database.

Resolution

It turns out that in this particular case, I was programmatically:

  1. Setting a field value
  2. Saving the record
  3. Opening a new related record

The issue was that the Save operation had not completed so the field I had modified was still considered dirty.

This allowed me to correct that issue and now things work as expected.

Up Next

My next article will outline how to work around the saving of the record while navigating to another record issue I ran into above.