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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Set value for lookUp f...
Power Apps
Answered

Set value for lookUp field in a quick create form with javascript code

(0) ShareShare
ReportReport
Posted on by 129

Hello,

 

I have a modeldriven App where I want to populate a lookup field (systemusers) when I open the form.

 

What I found was, that I need to have an array of lookup objects wich I want to insert.

 

this is the code in I try to use:

 

 

let lookupData = [
 {
 "entityType": "systemuser",
 "id": "{<the-ID-of-the-entity>}",
 "name": "Name Surname"
 }
]

formContext.getAttribute('lookupfield').setValue(lookupData);

 

 

Would be nice if someone has some Idea.

 

Thank you!

I have the same question (0)
  • Guido Preite Profile Picture
    1,490 Super User 2024 Season 1 on at

    the code is correct, what error do you get?

  • StephanPrnb Profile Picture
    129 on at

    It just does show the entry in the field (see Picture).

    StephanPrnb_0-1665745180129.png

     

  • Guido Preite Profile Picture
    1,490 Super User 2024 Season 1 on at

    can you share the full javascript where you inserted this code and a screenshot of the event handler where this js is executed? (onload or onchange)

  • cchannon Profile Picture
    4,702 Moderator on at

    Is the lookup you're setting the "parent" record? If so, there is a specific createFromEntity parameter you can set in the entityFormOptions of Xrm.Navigation.OpenForm. by setting this param, you will fill the lookup AND cause it to correctly respect cascade rules, if you have set them.

     

    https://learn.microsoft.com/en-us/power-apps/developer/model-driven-apps/clientapi/reference/xrm-navigation/openform

     

  • StephanPrnb Profile Picture
    129 on at

    Hi here is how I implemented the handler:

    StephanPrnb_0-1665993169013.png

     

    and here is the code:

    var StatusUpdate = window.StatusUpdate || {};
    (function () {
     this.formOnLoad = function (executionContext) {
     let formContext = executionContext.getFormContext();
     formContext.getAttribute('tpg_reportdate').setValue(new Date());
    
     /* Code to automaticly insert Sponsor in Status Quickform-Submitted to field / seems correct but doeasnt write person into field. */
     let projectId = formContext.getAttribute('tpg_msdyn_project').getValue()[0]['id']; // .slice(1, -1).toLowerCase() Tested. dont need it to work...
     let sponsor = getSponsor('msdyn_project', projectId);
     console.log(sponsor);
     formContext.getAttribute('tpg_submittedto').setValue(sponsor);
    
     };
    }).call(StatusUpdate);
    
    function getSponsor(tableName, entityId) {
     let lookupData = new Array();
     let lookupItem = new Object();
    
     Xrm.WebApi.retrieveRecord(tableName, entityId).then(
     function (success) {
     lookupItem.entityType = success['_tpgde_projectsponsor_value@Microsoft.Dynamics.CRM.lookuplogicalname'];
     lookupItem.name = success['_tpgde_projectsponsor_value@OData.Community.Display.V1.FormattedValue'];
     lookupItem.id = "{" + success['_tpgde_projectsponsor_value'] + "}";
    
     lookupData.push(lookupItem);
     },
     function (error) {
     console.log(error);
     }
     );
     return lookupData;
    }

     

    Thank you

  • StephanPrnb Profile Picture
    129 on at

    I don't use the Xrm.Navigation in this use case. I've posted the code in a reply below if you want to have a look.

     

    Thank you

  • Verified answer
    Guido Preite Profile Picture
    1,490 Super User 2024 Season 1 on at

    I assume that this line

     console.log(sponsor);

    returns you just the empty array, correct?

    Xrm.WebApi.retrieveRecord is async, this means that your line to set the value of the sponsor value get executed before the value are retrieved. Or you put the set inside the success, or you await the async function.

  • StephanPrnb Profile Picture
    129 on at

    The console log actually shows me the lookup object in the array:

     

    StephanPrnb_1-1665995806485.png

     

    But now I've set the value inside the success and everything works. Thank you very much!

     

    If someone comes along this here is my final code:

    'use strict';
    debugger;
    
    var StatusUpdate = window.StatusUpdate || {};
    (function () {
     this.formOnLoad = function (executionContext) {
     let formContext = executionContext.getFormContext();
     formContext.getAttribute('tpg_reportdate').setValue(new Date());
    
     /* Code to automaticly insert Sponsor in Status Quickform-Submitted to field / seems correct but doeasnt write person into field. */
     let projectId = formContext.getAttribute('tpg_msdyn_project').getValue()[0]['id']; // .slice(1, -1).toLowerCase() Tested. dont need it to work...
     setSponsor(formContext, 'msdyn_project', projectId); // set value inside function
     };
    }).call(StatusUpdate);
    
    function setSponsor(formContext, tableName, entityId) {
     let lookupData = new Array();
     let lookupItem = new Object();
    
     Xrm.WebApi.retrieveRecord(tableName, entityId).then(
     function (success) {
     lookupItem.entityType = success['_tpgde_projectsponsor_value@Microsoft.Dynamics.CRM.lookuplogicalname'];
     lookupItem.name = success['_tpgde_projectsponsor_value@OData.Community.Display.V1.FormattedValue'];
     lookupItem.id = "{" + success['_tpgde_projectsponsor_value'] + "}";
    
     lookupData.push(lookupItem);
    
     formContext.getAttribute('tpg_submittedto').setValue(lookupData);
     },
     function (error) {
     console.log(error);
     }
     );
    }
  • MA-03050915-0 Profile Picture
    2 on at

    Thank you!  I had spent a day and a half trying to setValue a lookup field, without success.   There are plenty other how-to-deal-with-a-lookup-field posts, but none of them worked.  I don't know if it was the { } round the ID, or the push of object into array, or the declarations outside the function, none of which I had, but - applying those after seeing your post sealed the deal.

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 Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the March Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
Vish WR Profile Picture

Vish WR 762

#2
11manish Profile Picture

11manish 640

#3
Valantis Profile Picture

Valantis 548

Last 30 days Overall leaderboard