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
Tasia Demuth 4Tasia Demuth 4 

filter visualforce email template tables

Hi all, 

I have a working Visualforce email template, but it lists all the contacts associated to an account rather than just contacts that have a value in the role field. 

Is there a way to filter the related records to just look at contacts with roles? Below is my code!


<messaging:emailTemplate subject="Opportunity has been won, please review roles for this account" recipientType="User" relatedToType="Account">
<messaging:htmlEmailBody >
<html>
<body>
<STYLE type="text/css">
               TH {font-size: 11px; font-face: arial;background: #CCCCCC; border-width: 1;  text-align: center } 
               TD  {font-size: 11px; font-face: verdana } 
               TABLE {border: solid #CCCCCC; border-width: 1}
               TR {border: solid #CCCCCC; border-width: 1}
         </STYLE>
                  <font face="arial" size="2">
        <p>Hello,</p>
        <p>{!relatedTo.Name} has closed won opportunities. Please review the contacts with roles related to this account.</p>
        <p><u>Account Information</u></p>
        <b>Account Name:</b> <a href="https://na32.salesforce.com/{!relatedTo.id}">{!relatedTo.name}</a>  
        <br/><b>Account Owner:</b> {!relatedTo.owner.name}
        <p/>                  
       <table border="1px" >
                 <tr > 
                     <th>Contact Name</th> <th>Role</th>
                  </tr>
    <apex:repeat var="contact" value="{!relatedTo.Contacts}">
       <tr>
           <td align="center">{!contact.Name}</td>
           <td align="center">{!contact.Role__c}</td>
       </tr>
    </apex:repeat>                 
       </table>
       <p />
       <table border="1px" >
                 <tr > 
                     <th>Opportunity Name</th> <th>Amount</th>
                  </tr>
    <apex:repeat var="opportunity" value="{!relatedTo.Opportunities}">
       <tr>
           <td align="center">{!opportunity.Name}</td>
           <td align="center">{!opportunity.amount}</td>
       </tr>
    </apex:repeat>                 
       </table>
       <p />
       <p> Thank you</p>
 </font>
       
        </body>
    </html>
</messaging:htmlEmailBody> 
</messaging:emailTemplate>


Thanks so much for your help!
Tasia

Best Answer chosen by Tasia Demuth 4
Tasia Demuth 4Tasia Demuth 4
Thank you Raj and Alain for your help!!

I was able to achieve what I was looking for with the below code in the email template.

The key was this line: <apex:outputPanel rendered="{!contact.Has_a_Role__c!='No'}">

This filtered out any contacts that did not have a role. 

Thanks again for your help! Your suggestions guided me to the solution. 
Tasia

<messaging:emailTemplate subject="Opportunity has been won, please review roles for {!relatedTo.name}" recipientType="User" relatedToType="Account">
<messaging:htmlEmailBody >
<html>
<body>
<STYLE type="text/css">
               TH {font-size: 11px; font-face: arial;background: #CCCCCC; border-width: 1;  text-align: center } 
               TD  {font-size: 11px; font-face: verdana } 
               TABLE {border: solid #CCCCCC; border-width: 1}
               TR {border: solid #CCCCCC; border-width: 1}
         </STYLE>
                  <font face="arial" size="2">
        <p>Hello,</p>
        <p>{!relatedTo.Name} has closed won opportunities. Please review the contacts with roles related to this account. The won opportunities for this year are also listed below.</p>
        <p><u>Account Information</u></p>
        <b>Account Name:</b> <a href="https://na32.salesforce.com/{!relatedTo.id}">{!relatedTo.name}</a>  
        <br/><b>Account Owner:</b> {!relatedTo.owner.name}
        <p/>                  
       <table border="1px" >
                 <tr > 
                     <th>Contact Name</th> <th>Role</th>
                  </tr>
    <apex:repeat var="contact" value="{!relatedTo.Contacts}">
    <apex:outputPanel rendered="{!contact.Has_a_Role__c!='No'}">
       <tr>
           <td align="center">{!contact.Name}</td>
           <td align="center">{!contact.Role__c}</td>
       </tr>
       </apex:outputPanel>
    </apex:repeat>                 
       </table>
       <p />
       <table border="1px" >
                 <tr > 
                     <th>Opportunity Name</th> <th>Amount</th>
                  </tr>
    <apex:repeat var="opportunity" value="{!relatedTo.Opportunities}">
    <apex:outputpanel rendered="{!opportunity.Opportunity_commissioned_this_year__c=True}">
       <tr>
           <td align="center">{!opportunity.Name}</td>
           <td align="center">{!opportunity.amount}</td>
       </tr>
    </apex:outputpanel>
    </apex:repeat>                 
       </table>
       <p />
       <p> Thank you</p>
 </font>
       
        </body>
    </html>
</messaging:htmlEmailBody> 
</messaging:emailTemplate>

All Answers

Raj VakatiRaj Vakati
The only way you can able to do it using the Visualforce Component with apex class and call the component in this email template 

Refer this link 

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_email_templates_with_apex.htm

http://sfdcsrini.blogspot.com/2014/07/visualforce-email-template-with-custom.html

http://amitsalesforce.blogspot.com/2017/05/how-to-create-visualforce-email.html
https://sfdcfanboy.com/2017/04/10/visualforce-email-template-with-custom-controller/
Alain CabonAlain Cabon
Hi,

You can also try to filter the rows (<tr>) if tricks of pure html with style="display: none;" and IFs work for your case but the contacts could be plentiful.
 

{!IF  ISBLANK ( contact.Role__c ) ,'','style="display: none;"'}

IMPORTANT Use ISBLANK instead of ISNULL in new formulas. ISBLANK has the same functionality as ISNULL, but also supports text fields. Salesforce will continue to support ISNULL, so you do not need to change any existing formulas.
 
<table border="1px"  {!IF relatedTo.Contacts.size > 0,'','style="display: none;"'}  >
      <tr > 
              <th>Contact Name</th> <th>Role</th>
      </tr>
    <apex:repeat var="contact" value="{!relatedTo.Contacts}">
       <tr  {!IF ISBLANK(contact.Role__c),'','style="display: none;"'} >
           <td align="center">{!contact.Name}</td>
           <td align="center">{!contact.Role__c}</td>
       </tr>
    </apex:repeat>                 
</table>

 
Tasia Demuth 4Tasia Demuth 4
Thank you Raj and Alain for your help!!

I was able to achieve what I was looking for with the below code in the email template.

The key was this line: <apex:outputPanel rendered="{!contact.Has_a_Role__c!='No'}">

This filtered out any contacts that did not have a role. 

Thanks again for your help! Your suggestions guided me to the solution. 
Tasia

<messaging:emailTemplate subject="Opportunity has been won, please review roles for {!relatedTo.name}" recipientType="User" relatedToType="Account">
<messaging:htmlEmailBody >
<html>
<body>
<STYLE type="text/css">
               TH {font-size: 11px; font-face: arial;background: #CCCCCC; border-width: 1;  text-align: center } 
               TD  {font-size: 11px; font-face: verdana } 
               TABLE {border: solid #CCCCCC; border-width: 1}
               TR {border: solid #CCCCCC; border-width: 1}
         </STYLE>
                  <font face="arial" size="2">
        <p>Hello,</p>
        <p>{!relatedTo.Name} has closed won opportunities. Please review the contacts with roles related to this account. The won opportunities for this year are also listed below.</p>
        <p><u>Account Information</u></p>
        <b>Account Name:</b> <a href="https://na32.salesforce.com/{!relatedTo.id}">{!relatedTo.name}</a>  
        <br/><b>Account Owner:</b> {!relatedTo.owner.name}
        <p/>                  
       <table border="1px" >
                 <tr > 
                     <th>Contact Name</th> <th>Role</th>
                  </tr>
    <apex:repeat var="contact" value="{!relatedTo.Contacts}">
    <apex:outputPanel rendered="{!contact.Has_a_Role__c!='No'}">
       <tr>
           <td align="center">{!contact.Name}</td>
           <td align="center">{!contact.Role__c}</td>
       </tr>
       </apex:outputPanel>
    </apex:repeat>                 
       </table>
       <p />
       <table border="1px" >
                 <tr > 
                     <th>Opportunity Name</th> <th>Amount</th>
                  </tr>
    <apex:repeat var="opportunity" value="{!relatedTo.Opportunities}">
    <apex:outputpanel rendered="{!opportunity.Opportunity_commissioned_this_year__c=True}">
       <tr>
           <td align="center">{!opportunity.Name}</td>
           <td align="center">{!opportunity.amount}</td>
       </tr>
    </apex:outputpanel>
    </apex:repeat>                 
       </table>
       <p />
       <p> Thank you</p>
 </font>
       
        </body>
    </html>
</messaging:htmlEmailBody> 
</messaging:emailTemplate>
This was selected as the best answer
Alain CabonAlain Cabon
Good point, Tasia.

When a problem is treated in a thorough way with a feedback and a solution that is really valuable for everyone on this forum.

There are so many questions where people showed no signs of life after a first suggestion that your gratitude is appreciated.