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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Javascript to grab rel...
Power Apps
Unanswered

Javascript to grab related record and set lookup field with that record (Model-Driven)

(0) ShareShare
ReportReport
Posted on by 193

Basically, I am creating new Specimen records from within a Collection, with a quick create form. The related Collection is here by default, since I am creating the Specimen under that Collection.

 

A Collection is related to one patient, and due to some complex record linking, I need a Specimen to be linked to that patient as well. I am trying to pre-populate the Related Patient field with the same Patient as the Collection, but I cannot figure out how to do this with Javascript. I have tried using an OnLoad event and grabbing the record from the "Related Collection" field, getting the Related Patient value, and then setting that value here, but cannot seem to get it to work. I am thinking that the OnLoad event occurs before the data on the form is populated and that is why it can't find a value.

cah2035_0-1669041459913.png

 

Any suggestions would be appreciated. Let me know if I can provide any clarifying info.

Categories:
I have the same question (0)
  • kylrntglr Profile Picture
    8 on at

    Hi, did you end up figuring this out? If so, what was the solution? Any JS code you used would be helpful. Thanks!

  • CAH2035 Profile Picture
    193 on at

    @kylrntglr I ended up not using a quick create form and used a regular form instead, but the same solution should apply. IIRC there were probably two things I needed to resolve:

    A) Access the 'Related Patient' of the Collection record where I was adding this new related Specimen record.

    B) Select that Related Patient as the default for this new related Specimen record.

     

    The first was resolved using the Xrm.WebApi.retrieveRecord method to access the Collection record, then use $expand to access the Related Patient record and $select to get the fields I needed.

     

    The second was resolved by using the retrieve information to make an array, which is then used to set the lookup field. A lookup field must be set using an array.

    var rPatientId = result.RelatedPatient.patientid;
    var rPatientName = result.RelatedPatient.studyid;
    var lookupPatient = new Array();
     lookupPatient[0] = new Object();
     lookupPatient[0].id = rPatientId;
     lookupPatient[0].name = rPatientName;
     lookupPatient[0].entityType = "patient";

     I can try and provide some more insight if you can elaborate on where you are stuck.

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the March Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 536

#2
WarrenBelz Profile Picture

WarrenBelz 426 Most Valuable Professional

#3
Haque Profile Picture

Haque 305

Last 30 days Overall leaderboard