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
scamarascamara 

Listing Contacts via Opportunity & Account using Visualforce

Hi,

I am trying to make a Visualforce email template which will display a list of Contacts from an Account associated with an Opportunity.  However, it seems that I am unable to do this.  Below is a snippet of my code.  I know that the problem lies within the <apex:repeat var="ct" value="{!relatedTo.Contacts}"> line since Contacts is not related to Opportunity.  However, I am not sure how to make this work.  I've tried Accounts.Contacts and other combinations that come to mind but I'm unable to get this working.  Since Opportunities - Accounts - Contacts are all related (in that order) I thought there would be a way to do this.   Any help on this would be greatly appreciated.  Thanks!  


<messaging:emailTemplate recipientType="User"    

relatedToType="Opportunity"    

subject="Contact list for: {!relatedTo.Account.Name}"    

replyTo="aaa@aaa.com" >    

 

<messaging:htmlEmailBody > 

<html>

<body>

         <STYLE type="text/css">

               TH {font-size: 12px; font-face: arial;background: #CCCCCC; border-width: 1;  text-align: left}

                TD  {font-size: 12px; font-face: arial}

                TABLE {border: solid #CCCCCC; border-width: 1}

               TR {border: solid #CCCCCC; border-width: 1}

         </STYLE>

             <font face="arial" size="2">

 

             <p>Dear <b>{!recipient.name}</b>,</p>

               <p>Below is a list of all contacts related to this opportunity.</p>

         <table border="0" cellpadding="5" cellspacing="0">

                 <tr >

                      <th>Name</th>

                  </tr>

    <apex:repeat var="ct" value="{!relatedTo.Contacts}">

 

<apex:outputPanel rendered="{!or(ct.User_Type__c = 'TrialUser',ct.Inst_Mkt_Type__c = 'Trial')}">

      <tr>          

<td>{!ct.Name}</td>

     </tr>    

</apex:outputPanel>    

</apex:repeat>
        </table>        

</font>            

</body>

</html>

</messaging:htmlEmailBody><messaging:plainTextEmailBody >"

Best Answer chosen by Admin (Salesforce Developers) 
sinsiterrulezsinsiterrulez

Create a VF component with controller to implement your logic & add that component in your email template

 

Go through this link for more info:

 

http://gokubi.com/archives/visual-force-email-templates-2

 

Let me know if you need anything or else mark it as accepted!!!

All Answers

jkucerajkucera

My experience is only with non-emails, but I assume the VF is the same - You can create a use <Apex:dataList> to display a list of records, and you can use ContactRoles as the link between Opportunity and Contact.

 

You can reference details from the contact in the role to populate the list.  This SOQL guide is a good help if you're trying to figure out the syntax for the query in the StandardSetController query locator:

http://blog.jeffdouglas.com/2010/02/22/soql-how-i-query-with-thee-let-me-count-the-ways/

sinsiterrulezsinsiterrulez

Create a VF component with controller to implement your logic & add that component in your email template

 

Go through this link for more info:

 

http://gokubi.com/archives/visual-force-email-templates-2

 

Let me know if you need anything or else mark it as accepted!!!

This was selected as the best answer
scamarascamara

Thanks for the replies. That site looks like exactly what I am looking for but I didn't have time to get to this today.  I will check it out tomorrow and let you know if this is 'resolved'.  Appreciate the responses!