Friday, May 13, 2011

CRM 2011 and getIsDirty JavaScript function

Hello Gang,

Every once in a while there arises a need to perform some sort of JavaScript calculation/data manipulation on a CRM form.  We all know that the folks at Microsoft were thoughtful enough to write a function that alerts you when you close of a form if there is modified data on it.  However it would be nice to be able to work around that helpful functionality at times.  Say you have code that formats a phone number on form load or performs a JavaScript calculation; it would be nice if the form didn't alert you every time you went to close it.

I pulled out my debugger and went to work.  After digging around for a while I found that this function is what gets called to figure out if the attribute is dirty:

 
function(){return this.$3N_1||this.$N_1!==this.get_value()}


So now it was really easy to solve the problem.  I just had to set the value of $N_1 to get_value().  Real easy...

this._attribute.$N_1 = this._attribute.get_value()

Next I had to create a function to call and perform all of this JavaScript goodness.  See below:

function DC_SetIsDirtyToFalse(attributeNameArray) {

    for(var x in attributeNameArray) {
        var attribute = Xrm.Page.data.entity.attributes.get(attributeNameArray[x]);
        /*set the appropriate values*/
        attribute._attribute.$N_1 = attribute._attribute.get_value();                
    }    
}
 
Now just copy and paste the above JavaScript code into a global.js file and call the DC_SetIsDirtyToFalse function right after you finish performing all of your onload calculations/manipulations.  You'll note that is consumes an array.  So you can pass in a number of attribute names and it will make sure that the getIsDirty() function will return false.

Cheers!

Bit Slinger 

3 comments:

  1. What if you wanted to set the IsDirty bit to true? Example your want to fire a workflow. How would you do that?

    ReplyDelete
  2. Hello Dan,

    Have you tried just setting an attribute to submit every time the form is saved? .setSubmitMode("always"). This will force the attribute to always submit a a value on save. I'll need more information on what you're trying to accomplish here to further assist you.

    ReplyDelete
  3. As from UR12 (Polaris) the updated code should be as follows:

    this._attribute.$V_1 = this._attribute.get_value()


    Yuval Rashelbach
    NextGen Consulting

    ReplyDelete