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() {
-
- var emailAttribute = Xrm.Page.getAttribute("new_email").getValue();
- CheckContactForEmailAddress(emailAttribute);
-
- }
-
- 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>" + ""
-
- 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;
-
- Xrm.Page.getAttribute("new_relatedcontact").setValue(contactval);
- } else {
- CheckLeadForEmailAddress(emailid);
- }
- }
-
- 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>" + ""
-
- 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;
-
- Xrm.Page.getAttribute("new_relatedlead").setValue(leadval);
- }
- }
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.
To create a new solution, provide the values to Yellow highlighted fields, as shown below.
Now, select Web Resources from the left navigation and upload JavaScript file, using New icon, which is highlighted Yellow.
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.
Now, select the email address field and Change Properties from the top ribbon.
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.
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.