You need to sign in to do that
Don't have an account?
Using apex:repeat to display a related list that has two custom objects associated
I am working on outputting a custom object to PDF by Email/VisualForce template. So far I have been successful with everything, but just ran across something that I need help with. As part of the PDF we are including four related lists that display on the objects page view. All the related lists contain fields from a single object except one. One of the related lists displays fields from two custom objects (Training_Attendee__c & Training_Course__c) on the page view. On the object this relationship is formed by a lookup field from the 'Training_Attendee__c' object to the 'Training_Course__c' object. This is what I am having trouble with. For the other three related lists I ran a apex:repeat and associated it with the single object that was needed and that worked fine...code example below:
<apex:repeat var="cx" value="{!relatedTo.Cases__r}"> <tr> <td> {!cx.CaseNumber} </td> <td> {!cx.Subject} </td> <td> {!cx.Case_Hours__c} </td> </tr> </apex:repeat>
Except now that I need to pull data from two objects I am not sure how to associate both objects to the same apex:repeat statement ...current code example for this particular related list is below:
<apex:repeat var="IGXTraining" value="{!relatedTo.Training_Attendee__r}"> <tr> <td> <apex:outputField value="{!IGXTraining.Name}" /> </td> <td> <apex:outputField value="{!IGXTraining.Training__c}" /> </td> <td> // value is from another custom object (Training_Course__c) // association is by lookup field on Training_Attendee__c to Training_Course__c {Location} </td> <td> // value is from another custom object (Training_Course__c) // association is by lookup field on Training_Attendee__c to Training_Course__c {StartDate} </td> <td> <apex:outputField value="{!IGXTraining.Course_Credit__c}" /> </td> </tr> </apex:repeat>
On the related list it automatically makes the association between the two objects so you can list fields from both on the page view:
Is it possible to associate a two object relationship using apex:repeat? If so any examples would really be appreciated. Thanks in advance!
I was able to solve this. Turns out the association was much easier to make than I expected. Initially I thought I would have to make a nested apex:repeat statement or a custom query, but the only thing that was needed was the correct dot notation. Here's a copy of the final code in case it helps someone down the road:
Brief explanation of the dot notation relationship: <apex:outputText value="{!IGXTraining.Training__r.Course__c}" />
All Answers
Hi,
It seems possible if both objects are related (watever u want to access in single table).
Then u can use relational query to fetch record from both objects.
Thanks,
I was able to solve this. Turns out the association was much easier to make than I expected. Initially I thought I would have to make a nested apex:repeat statement or a custom query, but the only thing that was needed was the correct dot notation. Here's a copy of the final code in case it helps someone down the road:
Brief explanation of the dot notation relationship: <apex:outputText value="{!IGXTraining.Training__r.Course__c}" />
Hi I am new to Apex and I have the same problem to solve as you have here so I am trying to reproduce your results substituting my stuff.
In your example <apex:repeat var="IGXTraining" value="{!relatedTo.Training_Attendee__r}">
Can you please tell me what the !relatedTo is? Is it a method in your class (class not shown) or a related list name?... I am a bit confused.
I have 2 custom objects related by a related list definition (called Holdings):
1. financial_acct__c
2.Holding__c
I need to display in a VisualForce Page:
financial acct: XXX
Holding1
Holding2
financial acct: YYY
Holding3
Holding4
Can you please help me out, I have been trying everything!