Skip to main content

Compare modified values on Save

Hi,

What if you want know whether a field is modified when you click on Save in CRM entity record form, it is simple, check the IsDirty property of the field, as in

if( crmForm.all.field.IsDirty == true) ...

what if you wanted to compare the modified value with the original value that was on Load when you click on Save, a pre-update callout/plugin seems to emerge as a candidate, but you can also do it through clientscript, all you need to do it so find a way to store the value of the original value of the field on Load and use it in your On Save script.

An obvious choice would be to use a hidden field, but for that we have to keep a spare attribute in the entity schema which means it will add an extra column to the entityExtensionbase table in CRM database, just to store a temporary value, doesn't sound good.

What we can do as an alternative is save it in a temporary html element that can be generated on-the-fly just when the form Loads, see how it is done:

On Load script:


initial_value = crmForm.all.field.DataValue;

var new_element = document.createElement('INPUT');

var outertext =
" < input type='hidden' " +
" id='temp_tag' value=' " +
initial_value +
"' />";

var idval = "temp_tag";
var typ = "hidden";



var tagId = document.createAttribute('id');
var tagType = document.createAttribute('type');


var outerHtml = document.createAttribute('outerHTML');

new_element.setAttribute(tagType,typ);
new_element.setAttribute(tagId,idval);
new_element.setAttribute(outerHtml,outertext);

document.getElementsByTagName('body')[0].appendChild(new_element);

This will add a new hidden element on the form when it loads, and it contains the value of your field, once you are done modifying it, you click on Save, you check whether it has been edited and compare it with the old value:

On Save script

if(crmForm.all.field.IsDirty==true)
{
var new_value = crmForm.all.field.DataValue;

//And you get the old value from the element you created on load
//Apparently, the element that you added is the last to be added to the form, hence you can retrieve it as the last element in the document's tag list

var element_array = document.getElementsById('input').length
var count = document.getElementsById('input').length;
var your_element = element_array[count-1];

//Now you have your element, read it's outerHTML property that contains your old value

var old_value = your_element.outerHTML;

//Please note that you get a tag and not the actual value in this outerHTML, so you have to do some string operation to retrieve it in its purest, non-adulterated form

// In my case, I split the tag string with single quote, returning an array of strings, just find out which place is your value, it was at position 5 in my case.

old_value = old_value.split("'")[5];

}

Now you have the new value as well as the old value, use it for any validation logic you want.

Comments

Anonymous said…
Hi Saurin,

Chanced upon your blog when I googled crmForm+isDirty! Welcome to the MSCRM blog world. Hope you blog more regularly than I do! :)

Popular posts from this blog

Using MS CRM Calendar in your custom ASP .NET pages *** Not fully Supported Customisations ***

Hi, Have you been creating custom pages in line with MS CRM for creating , viewing and updating entity records ? Have you placed controls such as Date picker Lookup on your custom page and have tried in vein to make those look like MS CRM ? I found a way to use the same MS CRM calendar control as it appears in the application... For getting this calendar on your asp .net page, follow the steps below: 1) Create a copy of the date.js javascript file located in your-server/crmsite-folder/_static/_controls/datetime folder, give the copy some name like date_myapp.js. 2) Open the following .js files from different sub folders in side the  your-server/crmsite-folder/_static      folder :       Global.js       encodedecode.js       xmlutil.js       util.js       remotecommand.js              Copy and whole content of each file one by one and keep pasting the same to the end of your date_myapp.js file. 3) Now open any CRM entity record page, for example a new Account crea

Capture and Handle event when one entity record is attached through a Many to Many relationship - MS CRM 4

If you have already dug up too much about trying to capture the event when an entity record in MS CRM is attached ~ linked to a record in many to many relationship. For example, if you have two entities : Supplier and Company in MS CRM where a supplier can do business with many companies and at the same time a Company can order stuff from many suppliers. What if you want to send some notification whenever a new supplier or an existing supplier is linked to a company; similarly you may want to send a notification when a Supplier signs a deal with 2-3 companies and gets linked to those. Some experts from MS CRM fraternity have suggested some ways to over ride this limitation of MS CRM that it does not expose any event for the above mentioned scenario, below is one of the examples:   http://ayazahmad.wordpress.com/2009/07/23/event-on-many-to-many-relationship-nn-in-crm-4/ But making changes to the default plugin model of MS CRM to add Associate or Disassociate event is not so eas

Creating Email through Plugin / SDK in CRM 4 - line breaks don't work

You may have noticed when you create email using a sample code like below in a plugin or for that matter any other SDK based assembley for MS CRM. Your line breaks : \n or \r or System.Environment.NewLine will not work.           email Email = new email();                          Email.to = new activityparty[] { toParty };                      Email.from = new activityparty[] { fromParty };                      Email.regardingobjectid = regardingObject;                      Email.subject = "Hello!!";                      Email.description = "Dear, line 1" +                                System.Environment.NewLine+                          "my message for your - line 2";                      Email.directioncode = new CrmBoolean();                      Email.directioncode.Value = true;                                            Guid _emailId = _service.Create(Email);                                            SendEmailRequest sendEmailReq