Last active
April 4, 2018 18:20
-
-
Save DynCon365/183aa092c4f643e68a97b3787e422f22 to your computer and use it in GitHub Desktop.
Dynamics 365/CRM | Set the value and text of a Lookup field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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