Saturday, March 10, 2012

Dirty fields causing headaches on form close / print preview.

I can’t even count the number of times that I’ve run into form issues where each and every time it's open/closed, even without changing data, it gives an alert that data has changed. This time, one of our clients was unable to use CRM's print preview feature due to this problem.

This is typically caused by something in the OnLoad event changing some data, but it can be a serious pain to track down, especially when it's not your original code! Here’s a nifty function that you can drop in at the end of your OnLoad event (or anywhere else) that will tell you exactly which fields are dirty:

function CheckDirtyFields() {
    var message = "The following fields are dirty:\n";
    Xrm.Page.data.entity.attributes.forEach(function (attribute, index) {
        if (attribute.getIsDirty() == true) {
            message += "  \u2219 " + attribute.getName() + "\n";
        }
    });
    alert(message);
}

The next question is how to remove that dirty field alert. The best option in general is to dig into the code and find out why that field is dirty in the first place and fix it, but if this can't (or shouldn't!) be fixed, refer to this blog post written by my colleague Caleb Skinner to clear up that dirty flag:


Hopefully this saves you some time when debugging dirty field issues!

No comments:

Post a Comment