Dynamics CRM‎ > ‎Javascript Library‎ > ‎

Javascript: Set value of a field equal to another field in MIcrosoft CRM

You can use Javascript to automatically set the value of a field in Microsoft CRM equal to another field.

Step1 - Create a web resource in CRM:
//  The following script is for text fields.  See additional code following for Option Sets
function Update()
{

var CRM_FORM_TYPE_CREATE = 1;
var CRM_FORM_TYPE_UPDATE = 2;
switch (crmForm.FormType)
{
case CRM_FORM_TYPE_CREATE:
crmForm.all.salutation.DataValue = crmForm.all.firstname.DataValue;
break;
 
case CRM_FORM_TYPE_UPDATE:
// do nothing
break;
}
}

Step 2 - In the field properties of the field you want to trigger the update, create an "event handler" that references the function above.
 
a) Add a reference to the web resource you created in step 1 by going to the field's "form libraries" settings and referencing the above code.
 
b) Then add an "event handler" for the firstname field's on change event. In the "function" field, use "Update". Note you don't have to put in the () in function field. 

 
 
 
 
 
***************************************************************************
Note:  For Option sets use the following code in place of step 1
function CopyData(fromField, toField)
// fromField is tthe name of the option set field the data is being copied from
// toField is the name of a text box the data is being copied into.
 
{
var CRM_FORM_TYPE_CREATE = 1;
var CRM_FORM_TYPE_UPDATE = 2;
//  this iis an option set you want the description not the value
var myChoice = crmForm.all(fromField).SelectedText;
switch (crmForm.FormType)
  {
  case CRM_FORM_TYPE_CREATE:
 
  crmForm.all(toField).DataValue = myChoice;
  break;
  case CRM_FORM_TYPE_UPDATE:
  // do nothing 
  break;
  }
}
Comments