Skip to content

Instantly share code, notes, and snippets.

@DynCon365
Last active April 4, 2018 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DynCon365/183aa092c4f643e68a97b3787e422f22 to your computer and use it in GitHub Desktop.
Save DynCon365/183aa092c4f643e68a97b3787e422f22 to your computer and use it in GitHub Desktop.
Dynamics 365/CRM | Set the value and text of a Lookup field
// Set the value and text of a Lookup field
// First, you need the GUID, Name and Entity Type stored as variables
// For this example, let's assume we have a "Parent Customer 2" field
// on a Contact and we want to populate it with the same value as the
// Parent Customer
// Get the GUID
var id = Xrm.Page.data.entity.attributes.get("primarycustomerid").getValue()[0].id;
// Get the Company Name
var name = Xrm.Page.data.entity.attributes.get("primarycustomerid").getValue()[0].name;
// Set the entity type - Account, Contact, Lead, Opportunity, etc.
var entityType = "Account";
// If the ID isn't null, then create a variable to store the new Object
if (id != null) {
var lookupVal = new Array();
lookupVal[0] = new Object();
lookupVal[0].id = guid;
lookupVal[0].name = name;
lookupVal[0].entityType = entityType;
// Set the value of the custom "Parent Customer 2" field
Xrm.Page.getAttribute("new_parentcustomerid2").setValue(lookupVal);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment