Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Apps - Power Apps Pro Dev & ISV
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!

  • MA-03050915-0 Profile Picture
    2 on at
    Re: Set value for lookUp field in a quick create form with javascript code

    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.

  • StephanPrnb Profile Picture
    129 on at
    Re: Set value for lookUp field in a quick create form with javascript code

    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);
     }
     );
    }
  • Verified answer
    Guido Preite Profile Picture
    1,488 Super User 2024 Season 1 on at
    Re: Set value for lookUp field in a quick create form with javascript code

    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
    Re: Set value for lookUp field in a quick create form with javascript code

    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

  • StephanPrnb Profile Picture
    129 on at
    Re: Set value for lookUp field in a quick create form with javascript code

    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

  • cchannon Profile Picture
    4,702 Super User 2025 Season 1 on at
    Re: Set value for lookUp field in a quick create form with javascript code

    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

     

  • Guido Preite Profile Picture
    1,488 Super User 2024 Season 1 on at
    Re: Set value for lookUp field in a quick create form with javascript code

    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)

  • StephanPrnb Profile Picture
    129 on at
    Re: Set value for lookUp field in a quick create form with javascript code

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

    StephanPrnb_0-1665745180129.png

     

  • Guido Preite Profile Picture
    1,488 Super User 2024 Season 1 on at
    Re: Set value for lookUp field in a quick create form with javascript code

    the code is correct, what error do you get?

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!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Congratulations to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Power Apps - Power Apps Pro Dev & ISV

#1
WarrenBelz Profile Picture

WarrenBelz 85 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 65 Super User 2025 Season 1

#3
mmbr1606 Profile Picture

mmbr1606 55 Super User 2025 Season 1

Overall leaderboard