Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Answered

Auto populate lookup field from other entity

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 ! 

  • RE: Auto populate lookup field from other entity

    Thank you so much Bipin

  • 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
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: Auto populate lookup field from other entity

    Yes, same code can be used for other lookup.

  • 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
    Bipin D365 28,964 Super User 2024 Season 1 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?

  • RE: Auto populate lookup field from other entity

    Hi Bipin ,

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

  • RE: Auto populate lookup field from other entity

    Thank you so much it is working .

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 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.

  • RE: Auto populate lookup field from other entity

    4718.Capture.PNGHI Bipin,

    I'm getting  below error,

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans