Tuesday, August 18, 2009

Capturing Previous Value on OnChange of Form Field in MS CRM 4.0

When you change the "Account Number" value, you want to populate a field called "Prior Account Number" with the previous value. The issue is that OnChange events happen AFTER the value has changed, so you can't copy the former value to the Prior Account Number field.

One solution is to use another field to store the previous value. Let's call this field accounttemp. (We will need to include this field on the form so we can access it via javascript, but we can hide it by either putting it on a hidden tab or using javascript to hide the field.)

Once we have our accounttemp and Prior Account Number field added to the Account form, we can add javascript to the OnChange event for the Account Number field. To make this happen we will need to make sure that the following steps happen in order:

1. Value of account temp field is copied to Prior Account Number field
2. Value of Account Number field is copied to the accounttemp field

Here's how that would look in Jscript:
crmForm.all.new_prioraccountnumber.DataValue=crmForm.all.new_accounttemp.DataValue;
crmForm.all.new_accounttemp.DataValue = crmForm.all.accountnumber.DataValue ;

The result is that the first time a value is entered into the Account Number field, the Prior Account Number field will remain blank, but the new value will be copied to the accounttemp field. The next time that the value of the Account number changes, the previous value will be copied to the Prior Account Number field.


Note—this will only work if the values are entered via the form. If you import this data from Scribe or the CRM import utility, the value will not be in the accounttemp field, so the process won't work for the initial change. The workaround is that when you import your account information, import the account number to the account number field AND the accounttemp field.

No comments: