web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Auto populate lookup field from other entity

(0) ShareShare
ReportReport
Posted on by 55

HI Everyone !

I need some suggestion and code for auto populate lookup field from other entity ,

ex: if i select parent account lead /existing account all the related field should populate in lead please give some code .

thank you in advance ! 

I have the same question (0)
  • Verified answer
    Bipin D365 Profile Picture
    28,985 Moderator on at

    Hi,

    Please try below code to set lookup field from Javascript

    // Create new lookup array

      var lookup = [];

       lookup[0] = {};

       lookup[0].id = record Guid;

       lookup[0].entityType = Entity Name;

       lookup[0].name = Primary field value;

      // Get and Set New Lookup

       var emergencyContact = formContext.getAttribute("msft_emergencycontact");

       emergencyContact.setValue(lookup);

  • Beginner Developer Profile Picture
    55 on at

    Hi Bipin thank you for reply actually i need to get value from parent account to lead ,

    Please check this code and let me know if any issue :

    function setLookup(executionContext) {

       var formContext = executionContext.getFormContext();

       var account = formContext.getAttribute('parentaccountid').getValue()[0].id.slice(1, -1);//get account id

       if (account != null) {

           Xrm.WebApi.online.retrieveRecord("account", "" + contact + "", "?$select=_new_country_value").then(

               function success(result) {

                   var _new_country_value = result["_new_country_value"];

                   var _new_country_value_formatted = result["_new_country_value@OData.Community.Display.V1.FormattedValue"];

                   var _new_country_value_lookuplogicalname = result["_new_country_value@Microsoft.Dynamics.CRM.lookuplogicalname"];

                   if (_new_country_value != null) {

                       var value = new Array();

                       value[0] = new Object();

                       value[0].id = _new_country_value;

                       value[0].name = _new_country_value_formatted;

                       value[0].entityType = _new_country_value_lookuplogicalname;

                       formContext.getAttribute("new_country").setValue(value); //set the counrty lookup field value in lead

                   }

                   else {

                       alert("No Contact");

                   }

               },

               function (error) {

                   Xrm.Utility.alertDialog(error.message);

               }

           );

       }

    }

  • Suggested answer
    Bipin D365 Profile Picture
    28,985 Moderator on at

    Hi,

    I updated your code. Please try and confirm if you get any error -

    function setLookup(executionContext) {

      var formContext = executionContext.getFormContext();

      var account = formContext.getAttribute('parentaccountid').getValue()[0].id.slice(1, -1);//get account id

      if (account != null) {

          Xrm.WebApi.online.retrieveRecord("account", account, "?$select=_new_country_value").then(

              function success(result) {

                  var _new_country_value = result["_new_country_value"];

                  var _new_country_value_formatted = result["_new_country_value@OData.Community.Display.V1.FormattedValue"];

                  var _new_country_value_lookuplogicalname = result["_new_country_value@Microsoft.Dynamics.CRM.lookuplogicalname"];

                  if (_new_country_value != null) {

                      var value = new Array();

                      value[0] = new Object();

                      value[0].id = _new_country_value;

                      value[0].name = _new_country_value_formatted;

                      value[0].entityType = _new_country_value_lookuplogicalname;

                      formContext.getAttribute("new_country").setValue(value); //set the counrty lookup field value in lead

                  }

                  else {

                      alert("No Contact");

                  }

              },

              function (error) {

                  Xrm.Utility.alertDialog(error.message);

              }

          );

      }

    }

  • Beginner Developer Profile Picture
    55 on at

    4718.Capture.PNGHI Bipin,

    I'm getting  below error,

  • Suggested answer
    Bipin D365 Profile Picture
    28,985 Moderator on at

    Hi,

    I fixed this error.

    Please try my code.

    CRM.webapi.retrieverecord second parameter you are passing contact which does not exist, instead you should pass account.

    This was your code mistake so I updated and replied with correct code.

  • Beginner Developer Profile Picture
    55 on at

    Thank you so much it is working .

  • Beginner Developer Profile Picture
    55 on at

    Hi Bipin ,

    Can this will be used when 2 lookup values i need to update also .

  • Suggested answer
    Bipin D365 Profile Picture
    28,985 Moderator on at

    Hi,

    What do you mean by 2 lookup value?

    Are you referring to multi entity lookup?

  • Beginner Developer Profile Picture
    55 on at

    HI Bipin,

    Actually 2fields values i need to populate for ex: state and country from one entity to other entity like A and B lookup from X entity to Y entity .

  • Suggested answer
    Bipin D365 Profile Picture
    28,985 Moderator on at

    Yes, same code can be used for other lookup.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
ManoVerse Profile Picture

ManoVerse 128 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 119

#3
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 69 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans