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 =[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.


Bit Slinger