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
Complete noviceComplete novice 

VF email template on Opportunity, can I display contacts from related account?

Hi, I have a VF email template on the Opportunity object. I've been asked to include contact information from the related account but I can't work out how to get this into the template.

This is the code:

messaging:emailTemplate subject="Renewal." recipientType="Contact" relatedToType="Opportunity"
 replyTo="xxx@xxx.org" >
 
<messaging:htmlEmailBody >        
    <html>
        <body>
        <apex:image id="Email_Header"
value="https://c.cs84.content.force.com/servlet/servlet.ImageServer?id=xxxxx&oid=xxxxx"
height="130" width="600"/>

         <STYLE type="text/css">
                  TH {font-size: 14px; font-face: arial;background: #FFFFFF; border-width: 0;  text-align: center }
               TD  {font-size: 14px; font-face: arial }
             
             </STYLE>
           <font face="arial" size="2">
           <br/>
           <br/>
      <p/>  Dear  
    <apex:repeat value="{!RelatedTo.OpportunityContactRoles}" var="ContactRole">
<apex:outputText value=" {!ContactRole.Contact.Name}" rendered="{!AND(ContactRole.isPrimary)}"></apex:outputtext>

</apex:repeat>,
            <br/>
            <br/>       
        {!RelatedTo.Account.Name}'s membership expires on <apex:outputText value=" {0, date,EEEE d MMMM yyyy}"> <apex:param value="{!relatedTo.Asset_Expires_When__c}"/> </apex:outputtext>.
                    <br/>
       
Our records show the information below about your company:
<br/>
            <br/>
             <td><b>•   </b> Company Size: {!relatedTo.Account.Number_of_Employees__c} employees</td>
             <br/>
             <td><b>•   </b> Turnover: {!relatedTo.Account.CurrencyIsoCode} {!relatedTo.Account.AnnualRevenue}</td>

<p/>  <apex:repeat var="opp" value="{!relatedTo.OpportunityLineItems}">  
       <tr>
       <td><b>•   </b> Membership type {!opp.PriceBookEntry.name}.</td> <br/>
        </tr>
      </apex:repeat>  
      <br/>
 <p/>  <apex:repeat var="acc" value="{!relatedTo.Account}">  
       <tr>
       <td><b>•   </b> Main Contact "{!relatedto.acc.Contacts}".</td> <br/>
        </tr>
      </apex:repeat> 


The part in italics is what I have added to try and include the contact related list but I get the error 'Invalid field acc for SObject Opportunity'.

I'm not good on code; is there a simple way to bring in a related list from a related object?

Any help is much appreciated!

Thanks.
Raj VakatiRaj Vakati
Yes 

Try this 

 <p/>  Dear  
    <apex:repeat value="{!RelatedTo.Account.Contacts}" var="contacts">

</apex:repeat >
Complete noviceComplete novice
Hi Raj, sorry, I wasn't very clear.  It's not the contact the email is being sent to I need to change. I need to add a list of the related contacts from the Account into the body of the email.

I have also tried this:
 <apex:repeat var="acc" value="{!relatedTo.Account}">  
       <tr>
       <td><b>•   </b> Main contact {!relatedto.Account.Contacts}.</td> <br/>
        </tr>
      </apex:repeat>  


But get the following error message: Aggregate Relationship is used in an unsupported complex expression containing 'Account.contacts'
Complete noviceComplete novice
Ok, it would appear I need a custom controller and a custom component.  Can anyone direct me to anywhere that will help me write this please?  Writing a new controller is beyond me and I don't know where to start.

Many thanks.
Complete noviceComplete novice
I've started a new question as it's probably clearer.