Introduction

Do you feel it annoying to enter First Name and Last Name in lead even when Existing Contact is selected already?? If yes, then this post will help you to auto populate these fields as soon as Existing Contact is selected. Lead entity contains fullname as primary field (key attribute), which is a combination for First and Last name. These field is used to create contact record if Existing Contact lookup is not selected under lead, so based on the default process when lead is qualified it will create a contact record with the value of these fields (We can write a plug-in to stop creating contact record if required)



Figure 1: Qualify

Requirement: We need to fill First Name and Last Name based on the Existing Contact lookup.

Solution: Existing Contact field is placed on the business process flow and logical name of the field is parentcontactid. We can write a quick script with the help of OData endpoints to get selected contact firstname and lastname and set it on the lead record. To call our script we need to place this field on lead form first. The following are the steps to implement this solution:
  1. function ParentContactId_OnChange()
  2. {
  3. if (Xrm.Page.getAttribute("parentcontactid") != null)
  4. {
  5. var ContactId = Xrm.Page.getAttribute("parentcontactid").getValue()[0].id;
  6. //utilize retrieveRecord method from REST library to get data based on contact id
  7. SDK.REST.retrieveRecord(
  8. ContactId,
  9. "Contact",
  10. null, null,
  11. function(contact)
  12. {
  13. //once we have data fill name fields
  14. Xrm.Page.getAttribute("firstname").setValue(contact.FirstName);
  15. Xrm.Page.getAttribute("lastname").setValue(contact.LastName);
  16. Xrm.Page.getAttribute("fullname").setValue(contact.FirstName + " " + contact.LastName);
  17. },
  18. function Error()
  19. {
  20. alert("There is error in reading contact data");
  21. });
  22. }
  23. }



Figure 3: Explore


Figure 4: Display


Figure 5: Lookup
Now when we will select Existing Contact it will auto populate Name field.


Figure 6: Name