Lookups and Javascript – Dynamics CRM

// Get Lookup
var LookupField = Xrm.Page.data.entity.attributes.get("FieldName");
// Get Lookup ID
// NOTE: To avoid possible errors, verfy that the lookup has value with:
//   If(LookupField != null)
var LookupID = Xrm.Page.data.entity.attributes.get("FieldName").getValue()[0].id;
// Get Lookup Value Name
// To avoid possible errors, verfy that the lookup has value with:
//   If(LookupField != null)
var LookupName = Xrm.Page.data.entity.attributes.get("FieldName").getValue()[0].name;
// Set Lookup Value
varNewLookupValue = new Array();
NewLookupValue[0] = new Object();
NewLookupValue[0].entityType = "EntityName";
NewLookupValue[0].id = RecordId; // RecordId= ID Of the record
NewLookupValue[0].name = "The name you want to set"; // Text that will appear in Lookup
Xrm.Page.getAttribute("FieldName").setValue(NewLookupValue)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s