How to Set a Lookup Field to Another Lookup Field on a Dynamics 365 Power Apps Form using JavaScript

12 Comments

There may be cases where you need to set a lookup field on a Dynamics 365 / Power Apps form, where you want to default the value to another lookup on the form using JavaScript to default the field value.

For example, on the Account form, we have the owner of the account (field OwnerId), and we have a custom field called My System User Field (new_mysystemuserfield) which is a system user lookup which looks like below:

Let’s say we want to set the System User field to the Owner Id field on load of the form, and perhaps add some additional business logic if we wanted to (your use case may be more complex):

Create a new web resource on the account form on load with the following code:

function OnLoad(context) {
   console.log("On Load");
    
   // First get the OwnerId lookup
   var OwnerId = FormContext.getAttribute("ownerid").getValue();
   var Id = OwnerId[0].id;
   var Name = OwnerId[0].name;
   var EntityType = OwnerId[0].entityType;
    
   // Create new lookup array
    
   var lookup = [];
    lookup[0] = {};
    lookup[0].id = Id;
    lookup[0].entityType = EntityType;
    lookup[0].name = Name;
 
   // Get and Set New Lookup
    var MySystemUserField = FormContext.getAttribute("new_mysystemuserfield");
    MySystemUserField.setValue(lookup);  
}

Now when opening the form, this field is defaulted from our Owner field:

 

THANKS FOR READING. BEFORE YOU LEAVE, I NEED YOUR HELP.
 

I AM SPENDING MORE TIME THESE DAYS CREATING YOUTUBE VIDEOS TO HELP PEOPLE LEARN THE MICROSOFT POWER PLATFORM.

IF YOU WOULD LIKE TO SEE HOW I BUILD APPS, OR FIND SOMETHING USEFUL READING MY BLOG, I WOULD REALLY APPRECIATE YOU SUBSCRIBING TO MY YOUTUBE CHANNEL.

THANK YOU, AND LET'S KEEP LEARNING TOGETHER.

CARL

https://www.youtube.com/carldesouza

 

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, Power BI, Azure, and AI.

carldesouza.comLinkedIn Twitter | YouTube

 

12 Responses to How to Set a Lookup Field to Another Lookup Field on a Dynamics 365 Power Apps Form using JavaScript

  1. Hi Carl! Thank you so much for this. As always, these posts are big help for the community.

    I’ve noticed a little change in the framework (I think). After initialization, you must create an object and it will work like a charm.

    lookup[0] = new Object();

    • Hi Arafat! That is interesting. It seems to be working for me still, but wondering if it’s version specific. I’ll keep an eye out for this, thanks so much for adding this detail. – Carl

  2. Hi Carl!!
    Amazing one here!!
    Is it possible to get other field properties of a lookup using javascript web resource??

    • Yes, I would like to know as well as Oluwasanmi Aina, is it possible to get other field properties of a lookup using a javascript (or other) web resourse? I have a custom form in which the user selects an account in a lookup field (or the new “customer” data type). I would like to auto populate the account number field from the account in the lookup field, into a text field on the custom form. Is this possible?

      • Hi Joan/Oluwasanmi,
        Its possible! check the script below I used to auto-populate the other fields (province and country) when the “city” field is selected.
        Javascript web resource:

        function CityAutoUpdate(executionContext) {
        var formContext = executionContext.getFormContext();

        if (formContext !== null) {

        var city = formContext.getAttribute(“city”).getValue();

        if(city != null)
        {

        Xrm.WebApi.retrieveRecord(‘city’, city[0].id, “?$select=name,_provinceid_value”).then(
        function success(result) {
        if (result != null) {
        var provinceid = result._provinceid_value;
        var lookup = new Array();
        lookup[0] = new Object();
        lookup[0].id = result._provinceid_value;
        lookup[0].name = result[“_provinceid_value@OData.Community.Display.V1.FormattedValue”];
        lookup[0].entityType = result[“_provinceid_value@Microsoft.Dynamics.CRM.lookuplogicalname”];

        if (lookup[0].entityType == null) {
        alert(“Province record not Found”);
        }

        formContext.getAttribute(“province”).setValue(lookup);

        if(provinceid != null){
        Xrm.WebApi.retrieveRecord(‘province’, provinceid, “?$select=name,_countryid_value”).then(
        function success(result) {
        if (result != null) {
        var countryid = result._countryid_value;
        var lookup1 = new Array();
        lookup1[0] = new Object();
        lookup1[0].id = result._countryid_value;
        lookup1[0].name = result[“_countryid_value@OData.Community.Display.V1.FormattedValue”];
        lookup1[0].entityType = result[“_countryid_value@Microsoft.Dynamics.CRM.lookuplogicalname”];

        if (lookup1[0].entityType == null) {
        alert(“Country record has not Found”);
        }

        formContext.getAttribute(“country”).setValue(lookup1);

        }
        },
        function (error) {
        //console.log(error.message);
        // handle error conditions
        }
        );

        }

        }
        },
        function (error) {
        //console.log(error.message);
        // handle error conditions
        }
        );

        }
        }
        }

    • Hi Oluwasanmi,
      It’s possible! check the script below I used to auto-populate the other fields (province and country) when the “city” field is selected.
      Javascript web resource:

      function CityAutoUpdate(executionContext) {
      var formContext = executionContext.getFormContext();

      if (formContext !== null) {

      var city = formContext.getAttribute(“city”).getValue();

      if(city != null)
      {

      Xrm.WebApi.retrieveRecord(‘city’, city[0].id, “?$select=name,_provinceid_value”).then(
      function success(result) {
      if (result != null) {
      var provinceid = result._provinceid_value;
      var lookup = new Array();
      lookup[0] = new Object();
      lookup[0].id = result._provinceid_value;
      lookup[0].name = result[“_provinceid_value@OData.Community.Display.V1.FormattedValue”];
      lookup[0].entityType = result[“_provinceid_value@Microsoft.Dynamics.CRM.lookuplogicalname”];

      if (lookup[0].entityType == null) {
      alert(“Province record not Found”);
      }

      formContext.getAttribute(“province”).setValue(lookup);

      if(provinceid != null){
      Xrm.WebApi.retrieveRecord(‘province’, provinceid, “?$select=name,_countryid_value”).then(
      function success(result) {
      if (result != null) {
      var countryid = result._countryid_value;
      var lookup1 = new Array();
      lookup1[0] = new Object();
      lookup1[0].id = result._countryid_value;
      lookup1[0].name = result[“_countryid_value@OData.Community.Display.V1.FormattedValue”];
      lookup1[0].entityType = result[“_countryid_value@Microsoft.Dynamics.CRM.lookuplogicalname”];

      if (lookup1[0].entityType == null) {
      alert(“Country record has not Found”);
      }

      formContext.getAttribute(“country”).setValue(lookup1);

      }
      },
      function (error) {
      //console.log(error.message);
      // handle error conditions
      }
      );

      }

      }
      },
      function (error) {
      //console.log(error.message);
      // handle error conditions
      }
      );

      }
      }
      }

  3. Guangzhoutime.com is a vibrant and reliable platform dedicated to
    showcasing the lifestyle, culture, and business landscape of Guangzhou.
    Whether you’re a visitor, an expat, or a local entrepreneur, this site provides everything you need to know
    about the city. From in-depth features on emerging industries and major events to insider guides on food,
    fashion, and living in Guangzhou, Guangzhoutime.com covers it all.
    It’s an essential resource for anyone looking to stay updated on the city’s
    rapid development and opportunities. With its professional reporting
    and user-friendly design, Guangzhoutime.com brings the spirit of Guangzhou to life and helps you navigate its dynamic
    urban environment with ease. Dive in and experience Guangzhou like never before!

Leave a Reply

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