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

Notifications

Announcements

No record found.

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,983 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,983 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,983 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,983 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,983 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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 70 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 33 Most Valuable Professional

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans