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 ! 

  • Beginner Developer Profile Picture
    55 on at
    RE: Auto populate lookup field from other entity

    Thank you so much Bipin

  • Beginner Developer Profile Picture
    55 on at
    RE: Auto populate lookup field from other entity

    see this ,

    // JavaScript source codefunction 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=_st_country_value,_st_state_value").then(

           function success(result) {

               var _new_country_value = result["_st_country_value"];

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

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

               var _new_state_value = result["_st_state_value"];

               var _new_state_value_formatted = result["_st_state_value@OData.Community.Display.V1.FormattedValue"];

               var _new_state_value_lookuplogicalname = result["_st_state_value@Microsoft.Dynamics.CRM.lookuplogicalname"];

               if (_new_country_value != null || _new_state_value != null) {

                   var Country = new Array();

                   var State = new Array1();

                   Country[0] = new Object();

                   State[0] = new Object1();

                   Country[0].id = _new_country_value;

                   State[0].id = _new_state_value

                   Country[0].name = _new_country_value_formatted;

                   State[0].name = _new_state_value_formatted;

                   Country[0].entityType = _new_country_value_lookuplogicalname;

                   State[0].entityType = _new_state_value_lookuplogicalname;

                   formContext.getAttribute("st_country").setValue(Country);

                   formContext.getAttribute("st_state").setValue(state); //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,981 Moderator on at
    RE: Auto populate lookup field from other entity

    Yes, same code can be used for other lookup.

  • Beginner Developer Profile Picture
    55 on at
    RE: Auto populate lookup field from other entity

    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,981 Moderator on at
    RE: Auto populate lookup field from other entity

    Hi,

    What do you mean by 2 lookup value?

    Are you referring to multi entity lookup?

  • Beginner Developer Profile Picture
    55 on at
    RE: Auto populate lookup field from other entity

    Hi Bipin ,

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

  • Beginner Developer Profile Picture
    55 on at
    RE: Auto populate lookup field from other entity

    Thank you so much it is working .

  • Suggested answer
    Bipin D365 Profile Picture
    28,981 Moderator on at
    RE: Auto populate lookup field from other entity

    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
    RE: Auto populate lookup field from other entity

    4718.Capture.PNGHI Bipin,

    I'm getting  below error,

  • Suggested answer
    Bipin D365 Profile Picture
    28,981 Moderator on at
    RE: Auto populate lookup field from other entity

    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);

              }

          );

      }

    }

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 225 Super User 2025 Season 1

#2
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 91

#3
Vahid Ghafarpour Profile Picture

Vahid Ghafarpour 78 Super User 2025 Season 1

Overall leaderboard

Product updates

Dynamics 365 release plans