Setting Lead name based on Existing Customer

Do you feel it annoying to enter First Name and Last Name in lead even 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 which is primary field (key attribute). It is combination for First and Last name. These fields is used to create contact record if Existing Contact lookup is not selected under lead, so based on 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 create contact record if required)


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. Following are the steps to implement this solution:

  • Create custom solution or open default solution by navigating Settings -> Customizations-> Customize the system
  • Add a new script web resource and upload SDK.REST.js library like below, this library comes with CRM SDK, you can find it under \SampleCode\JS\RESTEndpoint\JavaScriptRESTDataOperations\JavaScriptRESTDataOperations\Scripts

Note: If you have not downloaded CRM SDK then first download it from here and then extract it.


  • Create another Java Script web resource and let’s name it Lead.js, we need to place following code under Text Editor:
function ParentContactId_OnChange() {
	if (Xrm.Page.getAttribute('parentcontactid') != null) {

		var ContactId = Xrm.Page.getAttribute('parentcontactid').getValue()[0].id;

		//utilize retrieveRecord method from REST library to get data based on contact id




		null, null,

		function(contact) {

			//once we have data fill name fields



			Xrm.Page.getAttribute('fullname').setValue(contact.FirstName + ' ' + contact.LastName);


		function Error() {
			alert('There is error in reading contact data');

  • Save and close web resource
  • Double click on Lead form to open form editor by navigating Lead->Forms->Lead, under your solution, and drag and drop Parent Contact for lead on lead form from Field Explorer


  • Double click on this field now and add our both web resources and bind on change event using following steps in below screen:


  • Click on Display tab and uncheck following option, to hide our lookup field from lead form.


  • Save and Clos form.
  • Click on Publish All Customizations to publish all the changes.

Now when we will select Existing Contact in lead it will auto populate Name field.


8 thoughts on “Setting Lead name based on Existing Customer

  1. Pingback: Setting Lead name based on Existing Customer - Microsoft Dynamics CRM Community

  2. Idalette de Bruin

    This looks like a great solution. Can this be used to populate other fields from the contact like email address and mobile phone into the lead. Can this be used in similar way in opportunity?

    Many thanks

    1. himbap Post author

      Thank you for reading our post, Yes, we can do that as well and similarly this can also be done in opportunity. Thanks , make You can follow us to get our blog RSS Feed

      1. Gaurav G

        Hello but I am not able to autpopulate emailaddress field from contact to lead using your JScript code please help me..!!

  3. srani

    Hi I am implementing the same logic on selection of Exhisting Account selection to update same account name to Lead Name..but its not working out..In name field its showing “Undefined undefined.
    function Lead_name_Update_acct_OnChange() {
    if (Xrm.Page.getAttribute(‘parentaccountid’) != null) {
    var AccountId = Xrm.Page.getAttribute(‘parentaccountid’).getValue()[0].id;
    null, null,
    function(account) {
    Xrm.Page.getAttribute(‘fullname’).setValue(account.FirstName + ‘ ‘ + account.LastName);
    } , function Error() {
    alert(‘There is error in reading contact data’); }); }} Did i miss anything ..?

    1. himbap Post author

      Thank you for reading our post, Did you try to debug your code, what every code you pasted here sees ok, but first make sure you are getting result and then assign value to field. Thanks


Leave a Reply

Your email address will not be published. Required fields are marked *