Skip to main content

Notifications

Power Apps - Power Apps Pro Dev & ISV
Answered

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

  • mccardj3 Profile Picture
    mccardj3 20 on at
    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
    Kavya1 54 on at
    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
    Kavya1 54 on at
    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
    ryanspain 100 on at
    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
    ShriniA on at
    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
    ryanspain 100 on at
    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

Microsoft Kickstarter Events…

Register for Microsoft Kickstarter Events…

Announcing Our 2025 Season 1 Super Users!

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

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 145,508

#2
RandyHayes Profile Picture

RandyHayes 76,287

#3
Pstork1 Profile Picture

Pstork1 64,839

Leaderboard