+ Start a Discussion
Chad RitchieChad Ritchie 

Visualforce Related Objects - 2 levels

Hey guys, let's say contact is my primary object, but I then do a related lookup on Opportunity, which I have working, how would I do a lookup on a related object to Opportunity, let's call it Closings, can that be done? Here's my code: (The first bold part works, but the second doesn't)
THANKS!!
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<messaging:emailTemplate subject="TEST" recipientType="Contact" relatedToType="Contact" >
<messaging:htmlEmailBody >
<html>
<style type="text/css">
body {font-family: arial; size: 12pt;}
</style>
<body>
Dear {!relatedTo.Name},

<br>
<br>
<apex:repeat var="opp" value="{!relatedTo.Opportunities__r}">
    <table border="0">
           <tr>
               <td>{!opp.Name}</td>
           </tr>
    </table>
</apex:repeat>


<apex:repeat var="cls" value="{!relatedTo.Opportunities__r.Closings__r}">
    <table border="0">
           <tr>
               <td>{!cls.Name}</td>
           </tr>
    </table>
</apex:repeat>

 
Joseph PescatelloJoseph Pescatello
Did you try?
{!relatedTo.Opportunities__r.Closings__c}
Chad RitchieChad Ritchie
Good suggestion, but it returned the error:  Unknown property 'VisualforceArrayList.Closings__c'

I can get this to work with standard objects, like opportunity, but once I try to go 2 levels deep with a standard, then a custom object, it seems to fail everytime.
Raj VakatiRaj Vakati
It wnt work  like this .. you need to get the related data from the custome controller and you need to call it there 

.
Visualforce email templates can leverage custom controllers to render highly customized content. To do so, include a custom component in a Visualforce email template that uses that custom controller.

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_email_templates_with_apex.htm
https://salesforce.stackexchange.com/questions/196983/custom-visualforce-component-inside-visualforce-email-template-how-to-see-apex
http://amitsalesforce.blogspot.com/2017/05/how-to-create-visualforce-email.html