Get Lookup id / Text / EntityName in CRM using JavaScript

It is required sometimes to get Lookup Id / Text / Entity schema name of the selected record from Lookup. To get that follow the below task.

Task: Get record GUID, record Name and Entity Type from the Phone Call entity OnChange of To field.

Solution: Please follow the below steps,

Step 1: Create a web resource of type Jscript and name it as “new_phonecall”.

Step 2: Click on Text Editor, copy paste the below code.

function toOnChange(){
getLookupDetails("to");
}

function getLookupDetails(lookupSchemaName) {
var lookupObj = Xrm.Page.getAttribute(lookupSchemaName); //Check for Lookup Object
if (lookupObj != null) {
var lookupObjValue = lookupObj.getValue();//Check for Lookup Value
if (lookupObjValue != null) {
var lookupEntityType = lookupObjValue[0].entityType, //To get EntityName
lookupRecordGuid = lookupObjValue[0].id, // To get record GUID
lookupRecordName = lookupObjValue[0].name; //To get record Name 

if (lookupEntityType != null && lookupRecordGuid != null && lookupRecordName != null) {
Xrm.Utility.alertDialog("Entity Type : " + lookupEntityType + "\nRecord GUID : " + lookupRecordGuid + "\nRecord Name : " + lookupRecordName, null);
}
}
}
}

Step 3: Save & Publish Jscript web resource.

Step 4: Open phonecall form click on Form Properties, and attach Jscript Library “new_phonecall” to Form Libraries.

Step 5: Select Control “To” and Event “OnChange”. Select “new_phonecall” library and provide function name “toOnChange”.

PhoneCall Webresource

Step 6: Open any phone call record. Select any record from To lookup field. You can see the below output.

PhoneCall Output

Please provide your valuable comments.

Advertisement

5 thoughts on “Get Lookup id / Text / EntityName in CRM using JavaScript

  1. This nolonger works for CRM 2015 Online Update 1. the lookupObj.getValue() function returns an empty object [].
    Does someone know a fix?

    Like

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 )

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.