Tag Archives: Lookup

Set Lookup Value using JavaScript Tip

Everyone has seen the method of updating a CRM lookup field on a form, most examples look something like this.

// CRM 4
var value = new Array();
value[0] = new Object();
value[0].id = idValue;
value[0].name = textValue;
value[0].typename = typeValue;

crmForm.all.fieldName.DataValue = value;

// CRM 2011
var value = new Array();
value[0] = new Object();
value[0].id = idValue;
value[0].name = textValue;
value[0].entityType = typeValue;

Xrm.Page.getAttribute("fieldName").setValue(value);

How about doing it on one line like this instead.


// CRM 4
crmForm.all.field.DataValue = [{id: idValue, name: textValue, typename: typeValue}];

// CRM 2011
Xrm.Page.getAttribute("fieldName").setValue( [{id: idValue, name: textValue, entityType: typeValue}]);

There is a short but interesting article about JavaScript called Seven JavaScript Things I Wish I Knew Much Earlier In My Career by Christian Heilmann if you have some spare time to read.

Advertisements