Cry about...
Dynamics CRM 2011 How To
How to get or set the value of a lookup field
The value of a field that is a lookup-field is obtained the same way as any other field, i.e.:
var value = Xrm.Page.getAttribute("lookupField").getValue();
what is different is how to use what is returned:
if (value) {
var name = value[0].name;
var id = value[0].id;
var entityType = value[0].entityType;
}
where:
name
- Is the value the user sees displayed in the field.
id
- Is the logical guid that identifies the record.
entityType
- Is the logical entity name of the entity being referenced.
To create a new lookup value use either:
var newValue = new Array();
newValue[0] = new
Object();
newValue[0].id = recordId;
newValue[0].name =
displayText;
newValue[0].entityType = logicalEntityName;
Xrm.Page.getAttribute("lookupField").setValue(newValue);
or this can be collapsed down to:
Xrm.Page.getAttribute("lookupField").setValue(
[{id: recordId, name:displayText, entityType:logicalEntityName}]);
These notes have been tested with Microsoft Dynamics CRM 2011, and may apply to other versions as well.
About the author: Brian Cryer is a dedicated software developer and webmaster. For his day job he develops websites and desktop applications as well as providing IT services. He moonlights as a technical author and consultant.