function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SeanCoSeanCo 

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!

Best Answer chosen by Admin (Salesforce Developers) 
SeanCoSeanCo

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:

 

 

<apex:repeat var="IGXTraining" value="{!relatedTo.Training_Attendee__r}">
   
   <tr>
    <td>
       <apex:outputText value="{!IGXTraining.Contact__r.Name}" />
    </td>
    
    <td>
      <apex:outputText value="{!IGXTraining.Training__r.Course__c}" />
    </td>
    <td>
       <apex:outputText value="{!IGXTraining.Training__r.Training_Location__c}" />
    </td>
    <td>   
       <apex:outputText value="{0,date,MM'/'dd'/'yyyy}">
    		<apex:param value="{!IGXTraining.Training__r.Training_Start_Date__c}" /> 
       </apex:outputText>
    </td>
    
    <td>
       <apex:outputField value="{!IGXTraining.Course_Credit__c}" />
    </td>
   </tr>
   
</apex:repeat>

 

Brief explanation of the dot notation relationship:  <apex:outputText value="{!IGXTraining.Training__r.Course__c}" />

 

 

  • !IGXTraining = is the variable declared in the initial apex:repeat and associated to the 'Training_Attendee__r' custom object/related list.
  • Training__r = is the custom object associated to 'Training_Attendee__r' by a Master-Detail relationship. 
  • Course__c = is field on the 'Training__c' custom object.

 

 

 

All Answers

Alok_NagarroAlok_Nagarro

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,

SeanCoSeanCo

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:

 

 

<apex:repeat var="IGXTraining" value="{!relatedTo.Training_Attendee__r}">
   
   <tr>
    <td>
       <apex:outputText value="{!IGXTraining.Contact__r.Name}" />
    </td>
    
    <td>
      <apex:outputText value="{!IGXTraining.Training__r.Course__c}" />
    </td>
    <td>
       <apex:outputText value="{!IGXTraining.Training__r.Training_Location__c}" />
    </td>
    <td>   
       <apex:outputText value="{0,date,MM'/'dd'/'yyyy}">
    		<apex:param value="{!IGXTraining.Training__r.Training_Start_Date__c}" /> 
       </apex:outputText>
    </td>
    
    <td>
       <apex:outputField value="{!IGXTraining.Course_Credit__c}" />
    </td>
   </tr>
   
</apex:repeat>

 

Brief explanation of the dot notation relationship:  <apex:outputText value="{!IGXTraining.Training__r.Course__c}" />

 

 

  • !IGXTraining = is the variable declared in the initial apex:repeat and associated to the 'Training_Attendee__r' custom object/related list.
  • Training__r = is the custom object associated to 'Training_Attendee__r' by a Master-Detail relationship. 
  • Course__c = is field on the 'Training__c' custom object.

 

 

 

This was selected as the best answer
JimmyMacJimmyMac

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!

 

 

 

 

jesse1.393977588133575E12jesse1.393977588133575E12
I'm having the same issue. What is relatedTo? Is that just a custom field on your controller? Is there any way to do this with a standard controller without extensions?