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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / How to auto fill looku...
Power Apps
Unanswered

How to auto fill lookup field value in model driven app

(0) ShareShare
ReportReport
Posted on by

Hi,

I am create model driven app and have two tables name as “Equipment” and “Equipment Order”.

Equipment table contain equipment, equipmentType, description, price etc.

Equipment Order table contain orderRef, equipment (lookup field), equipmentType etc.

 

My scenario is,

Equipment Order; when I select equipment(lookup) then auto filled with its equipmentType value. How could I do this in model driven app using JavaScript?

 

I appreciate your kind support.

 

Thank you

#modeldrivenapp #powerapps

I have the same question (0)
  • ryanspain Profile Picture
    100 on at

    Hey @ShriniA,

     

    You can achieve this in a model-driven Power App using the below high-level steps:

    1. Create a JavaScript web resource that contains the necessary JavaScript to set the Equipment Type field on the Equipment Order. Assuming that Equipment Type is a Choice (Option set) field, you can set its value using the `setValue` client API. Where you see `formContext` in the sample code, you just need to remember to call `getFormContext` on the `executionContect` that you'll pass into this JavaScript function when completing the next step.
    2. Configure model-driven app form event handlers to call your custom JavaScript code: The idea here is that you would register an event handler for the Equipment (Lookup) on the Equipment Order form that would call a JavaScript function call say `onEquipmentChanged` in the web resource you created. This is all done from the model-driven apps form.

    Give it a shot!
    Ryan

  • ShriniA Profile Picture
    on at

    @ryanspain 

    In Equipment table, equipmentType is a choice field. When I call it in Equipment Order table, I create equipmentType as a lookup field. Is that correct or what kind of field that I want to create it on Equipment Order table?

     

    Ex: 

    function onEquipmentChanged(executionContext){

    var formContext = executionContext.getFormContext();

    formContext.getAttribute(“equipmenttype”).setValue();

    }

     

  • Verified answer
    ryanspain Profile Picture
    100 on at

    Hey @ShriniA,

     

    I'm assuming that, when a user fills in the Equipment lookup on the new Equipment Order form, you want to simply map the Equipment Type from that selected Equipment to the new Equipment Order.

     

    If my assumption is correct, you need to do a couple of things in your onEquipmentChanged JavaScript method. I've copied a code sample below with some inline comments explaining what is being done.

    function onEquipmentChanged(executionContext){
     
     // get the form context from the execution context
     var formContext = executionContext.getFormContext();
     
     // get the selected Equipment records Equipment Type using the Web API
     var selectedEquipmentRef = formContext.getAttribute("equipment_field_name").getValue()[0];
    
     var selectedEquipment = await Xrm.WebApi.retrieveRecord(
     selectedEquipmentRef.entityType,
     selectedEquipmentRef.id,
     "?$select=equipment_type_field_name");
    
     var selectedEquipmentType = selectedEquipment.equipment_type_field_name;
    
     // set the Equipment Type field on the form
     formContext.getAttribute("equipment_field_name").setValue(selectedEquipmentType);
    }

    The documentation pages I used to remember/verify the syntax were:

    • getValue: To get the Equipment lookup value from the field on the form.
    • setValue: To set the Equipment Type choice field value on the form.
    • retrieveRecord: To retrieve the Equipment record that was selected via the lookup.

    Take note that you'll need to replace the reference to the field names that I put in like equipment_type_field_name and equipment_field_name.

    Hope this helped!

  • Kavya1 Profile Picture
    54 on at

    Hi @ShriniA 

    Have you got solution for your issue?

  • Kavya1 Profile Picture
    54 on at

    Hi @ryanspain 

    I have managed to add your code, however I am getting "web resource method does not exist" error. I have changed the field names accordingly and added to the form library and configured the event handler.

  • mccardj3 Profile Picture
    20 on at

    Hi @ryanspain ,

     

    I have a similar problem I’m trying to solve, I think your solution might work for me too but I’m not sure. I’d really appreciate it if you could take a look

     

    https://powerusers.microsoft.com/t5/Microsoft-Dataverse/Create-a-calculated-column-to-sum-up-values-in-Choices-columns/m-p/2285344#M32291

     

    thanks

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 717 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 329 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard