If you’ve ever tried using JavaScript with lookup values in CRM I’m sure you will have found that they are quite a lot different from the other types of attributes. With only a small amount of JavaScript code you can retrieve the following values from any lookup value.
Below is the code I used to get those 4 values:
Here’s an explanation of the code:
// Defines the Lookup field Value.
var lookupItem = crmForm.all.mag_productid.DataValue;
// Check to make sure the Lookup field contains data (if there is no data and you attempt to populate the value fields you will get an error message).
if (lookupItem != null) {
// Populate the field “Text Value” with the text value of the lookup field. (The name of the record you have selected)
crmForm.all.mag_textvalue.DataValue = lookupItem[0].name;
// Populate the field “Entity Type” with the schema name of the lookup field. (The type of record displayed e.g. “product”)
crmForm.all.mag_entitytype.DataValue = lookupItem[0].typename;
// Populate the field “GUID” with the GUID value of the lookup field. (The unique ID given to the record you have selected)
crmForm.all.mag_guid.DataValue = lookupItem[0].id;
// Populate the field “Entity Type Code” with the entity type code of the lookup field. (The unique number value given to the entity)
crmForm.all.mag_entitytypecode.DataValue = lookupItem[0].type;
}
To use this code:
var lookupItem = crmForm.all.lookupfield.DataValue;
if (lookupItem != null) {
crmForm.all.textvalue.DataValue = lookupItem[0].name;
crmForm.all.entitytype.DataValue = lookupItem[0].typename;
crmForm.all.guid.DataValue = lookupItem[0].id;
crmForm.all.entitytypecode.DataValue = lookupItem[0].type;
}
Now it’s unlikely that this code would be used as is, but hopefully with a little imagination and creativity you will be able to utilise it to make your CRM system more dynamic and smooth flowing.
If you are looking for more JavaScript code please look through the other Magnetism blog posts, or post here and we’ll see if we can help you out.