cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
ShriniA
Helper I
Helper I

How to auto fill lookup field value in model driven app

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

3 REPLIES 3
ryanspain
Helper II
Helper II

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

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

}

 

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!

Helpful resources

Announcements
Power Apps News & Annoucements carousel

Power Apps News & Announcements

Keep up to date with current events and community announcements in the Power Apps community.

Community Call Conversations

Introducing the Community Calls Conversations

A great place where you can stay up to date with community calls and interact with the speakers.

Power Apps Community Blog Carousel

Power Apps Community Blog

Check out the latest Community Blog from the community!

Users online (5,303)