Skip to main content

Notifications

Community site session details

Community site session details

Session Id : SYGDZEdumpcG0Uu35Yy+ua
Power Apps - Power Apps Pro Dev & ISV
Answered

How to auto fill lookup field value in model driven app

Like (0) ShareShare
ReportReport
Posted on 13 Aug 2022 11:07:31 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

  • mccardj3 Profile Picture
    20 on 08 Aug 2023 at 09:39:08
    Re: How to auto fill lookup field value in model driven app

    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

  • Kavya1 Profile Picture
    54 on 25 May 2023 at 06:27:41
    Re: How to auto fill lookup field value in model driven app

    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.

  • Kavya1 Profile Picture
    54 on 25 May 2023 at 06:24:38
    Re: How to auto fill lookup field value in model driven app

    Hi @ShriniA 

    Have you got solution for your issue?

  • Verified answer
    ryanspain Profile Picture
    100 on 17 Aug 2022 at 12:52:59
    Re: How to auto fill lookup field value in model driven app

    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!

  • ShriniA Profile Picture
    on 16 Aug 2022 at 00:15:14
    Re: How to auto fill lookup field value in model driven app

    @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();

    }

     

  • ryanspain Profile Picture
    100 on 15 Aug 2022 at 19:53:05
    Re: How to auto fill lookup field value in model driven app

    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

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

Thomas Rice – Community Spotlight

We are honored to recognize Thomas Rice as our March 2025 Community…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 146,508 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 65,369 Most Valuable Professional

Leaderboard
Loading started
Loading complete