Dynamics CRM - Find Related Contact's Reference And Link It To Custom Entity Using JavaScript

In this article, we are going to see how to query the entity's Contact and Lead with the email address of a custom entity and to link the reference of those matching "Contact and Lead". As soon as the user fills in the email address field and moves on to the next field, it should autofill the Related Contact and Related Lead.

Dynamics CRM

The solution is given below. Just a simple JavaScript will do the magic.

In this example, we are using XRMServiceToolKit.min.js and FetchXML to query Contact and Lead.

Filename - ContactLookupByEmail.js 

  1. function GetEmailAddress() {  
  2.     //debugger;    
  3.     var emailAttribute = Xrm.Page.getAttribute("new_email").getValue();  
  4.     CheckContactForEmailAddress(emailAttribute);  
  5.     //alert(emailAttribute);     
  6. }  
  7. // Function to check the Contact entity matching with the given email address    
  8. function CheckContactForEmailAddress(emailid) {  
  9.     var contactFetchXML = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" + " <entity name='contact'>" + " <attribute name='fullname' />" + " <attribute name='telephone1' />" + " <attribute name='contactid' />" + " <attribute name='emailaddress1' />" + " <order attribute='fullname' descending='false' />" + " <filter type='and'>" + " <condition attribute='emailaddress1' operator='eq' value='" + emailid + "' />" + " </filter>" + " </entity>" + "</fetch>" + ""  
  10.     //To fetch the Contact matching the given email address   
  11.     var contactRecords = XrmServiceToolkit.Soap.Fetch(contactFetchXML);  
  12.     if (contactRecords.length > 0) {  
  13.         var contactval = new Array();  
  14.         contactval[0] = new Object();  
  15.         contactval[0].id = contactRecords[0].id;  
  16.         contactval[0].name = contactRecords[0].attributes.fullname.value;  
  17.         contactval[0].entityType = contactRecords[0].logicalName;  
  18.         //Contact with matching email address is now assigned to Related Contact    
  19.         Xrm.Page.getAttribute("new_relatedcontact").setValue(contactval);  
  20.     } else {  
  21.         CheckLeadForEmailAddress(emailid);  
  22.     }  
  23. }  
  24. // Function to check the Lead entity matching with the given email address   
  25. function CheckLeadForEmailAddress(emailid) {  
  26.     var leadFetchXML = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" + " <entity name='lead'>" + " <attribute name='fullname' />" + " <attribute name='companyname' />" + " <attribute name='leadid' />" + " <attribute name='telephone1' />" + " <order attribute='fullname' descending='false' />" + " <filter type='and'>" + " <condition attribute='emailaddress1' operator='eq' value='" + emailid + "' />" + " </filter>" + " </entity>" + "</fetch>" + ""  
  27.     //To fetch the Lead matching the given email address   
  28.     var leadRecords = XrmServiceToolkit.Soap.Fetch(leadFetchXML);  
  29.     if (leadRecords.length > 0) {  
  30.         var leadval = new Array();  
  31.         leadval[0] = new Object();  
  32.         leadval[0].id = leadRecords[0].id;  
  33.         leadval[0].name = leadRecords[0].attributes.fullname.value;  
  34.         leadval[0].entityType = leadRecords[0].logicalName;  
  35.         //Lead with matching email address is now assigned to Related Lead     
  36.         Xrm.Page.getAttribute("new_relatedlead").setValue(leadval);  
  37.     }  
  38. }   

To add JavaScript file to Web Resource in Microsoft Dynamics CRM, please follow the steps given below.

Go to the Top Ribbon, select the Settings icon and from there, click Solutions to create new custom solution.

Dynamics CRM

 To create a new solution, provide the values to Yellow highlighted fields, as shown below.

Dynamics CRM

Now, select Web Resources from the left navigation and upload JavaScript file, using New icon, which is highlighted Yellow.

Dynamics CRM

Click Form Editor of the Event Registration form to configure, how and when JavaScript function should be called on change event of Email Address field.

Dynamics CRM
Now, select the email address field and Change Properties from the top ribbon.

Dynamics CRM

Select the Events tab and include all the highlighted JavaScript files by clicking Add icon. Most important thing is in Event Handlers section. Select the Event OnChange and map the GetEmailAddress() from the ContactLookupByEmail.JS.

Dynamics CRM

Finally click OK. Now, save the form and you can see the auto population of Related Contact and Lead on change of the E-mail Address field.