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.

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
- function GetEmailAddress() {
- //debugger;
- var emailAttribute = Xrm.Page.getAttribute("new_email").getValue();
- CheckContactForEmailAddress(emailAttribute);
- //alert(emailAttribute);
- }
- // Function to check the Contact entity matching with the given email address
- function CheckContactForEmailAddress(emailid) {
- 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>" + ""
- //To fetch the Contact matching the given email address
- var contactRecords = XrmServiceToolkit.Soap.Fetch(contactFetchXML);
- if (contactRecords.length > 0) {
- var contactval = new Array();
- contactval[0] = new Object();
- contactval[0].id = contactRecords[0].id;
- contactval[0].name = contactRecords[0].attributes.fullname.value;
- contactval[0].entityType = contactRecords[0].logicalName;
- //Contact with matching email address is now assigned to Related Contact
- Xrm.Page.getAttribute("new_relatedcontact").setValue(contactval);
- } else {
- CheckLeadForEmailAddress(emailid);
- }
- }
- // Function to check the Lead entity matching with the given email address
- function CheckLeadForEmailAddress(emailid) {
- 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>" + ""
- //To fetch the Lead matching the given email address
- var leadRecords = XrmServiceToolkit.Soap.Fetch(leadFetchXML);
- if (leadRecords.length > 0) {
- var leadval = new Array();
- leadval[0] = new Object();
- leadval[0].id = leadRecords[0].id;
- leadval[0].name = leadRecords[0].attributes.fullname.value;
- leadval[0].entityType = leadRecords[0].logicalName;
- //Lead with matching email address is now assigned to Related Lead
- Xrm.Page.getAttribute("new_relatedlead").setValue(leadval);
- }
- }







Join the conversation! Your thoughts help the community grow.