cancel
Showing results for 
Search instead for 
Did you mean: 
Reply

how to copy data from one table to another when selecting a lookup field in model driven app

Hello everyone

 

I am trying to get the data of a related record that i select in a lookup. So when the lookup field is selected, that the entire form gets filled up with the data that comes from that related record in the lookup field. Can anyone help me to fix

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisPiasecki
Super User
Super User

Hi @jorenchristiaen,

 

Do you need to save the same data from the related record, or do you just need to view the related data? If you just need to view related data, you could create a Quick View form for the related table and add it to your form. The quick view form would show up whenever the Lookup has a selected record populated.

 

If you need to populate data from a related entity, you can define Attribute Mappings on the 1:N relationship. If on the related record form you have a subgrid to this entity and click + (Add New), when it opens the create new form it will pre-populate the values of the related record including the lookup. Note that this will not auto-populate if you are just creating a new record without coming from the related record.

 

The other way to populate the fields is with a JavaScript web resource, registered to the OnChange event of the Lookup field. You would retrieve the related record using the Lookup value selected. Then you can set the form values using the related record you retrieved. 

 

---
Please click Accept as Solution if my post answered your question. This will help others find solutions to similar questions. If you like my post and/or find it helpful, please consider giving it a Thumbs Up.

 

View solution in original post

4 REPLIES 4
ChrisPiasecki
Super User
Super User

Hi @jorenchristiaen,

 

Do you need to save the same data from the related record, or do you just need to view the related data? If you just need to view related data, you could create a Quick View form for the related table and add it to your form. The quick view form would show up whenever the Lookup has a selected record populated.

 

If you need to populate data from a related entity, you can define Attribute Mappings on the 1:N relationship. If on the related record form you have a subgrid to this entity and click + (Add New), when it opens the create new form it will pre-populate the values of the related record including the lookup. Note that this will not auto-populate if you are just creating a new record without coming from the related record.

 

The other way to populate the fields is with a JavaScript web resource, registered to the OnChange event of the Lookup field. You would retrieve the related record using the Lookup value selected. Then you can set the form values using the related record you retrieved. 

 

---
Please click Accept as Solution if my post answered your question. This will help others find solutions to similar questions. If you like my post and/or find it helpful, please consider giving it a Thumbs Up.

 

nandiishdave5
Regular Visitor

Thanks for the question and answer both. It helped me to solve my problem. 

 

For second solution that you have given I have written simple js code and put it to field OnChange Event. 

 

function OnChangeEvent(executionContext, lookupfield, namefield) {
  var formContext = executionContext.getFormContext();
  if (formContext.getAttribute(lookupfield).getValue() != null) {
    formContext
      .getAttribute(namefield)
      .setValue(formContext.getAttribute(lookupfield).getValue()[0].name);
  } else {
    formContext.getAttribute(namefield).setValue("");
  }
}
 
Please refer it if anyone needs help 🙂
nandiishdave5
Regular Visitor

Thanks for the question. It wanted to do same thing. 

 

For second solution that you have given I have written simple js code and put it to field OnChange Event. 

 

function OnChangeEvent(executionContextlookupfieldnamefield) {
  var formContext = executionContext.getFormContext();
  if (formContext.getAttribute(lookupfield).getValue() != null) {
    formContext
      .getAttribute(namefield)
      .setValue(formContext.getAttribute(lookupfield).getValue()[0].name);
  } else {
    formContext.getAttribute(namefield).setValue("");
  }
}
Hopefully it will help to someone who needs similar solution 🙂
Please refer it if anyone needs help 

 

Hi @nandiishdave5 

 

Any chance you could elaborate on how to use this Js? im really struggling to get it to work 🙂

 

Thanks!

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 (4,435)