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
ahonahon 

Email Template - Related Lists

How do I display two related lists of an object on an email template? For example, a related list with relevant related lists nested under each row. Does not have to invovled code.

Here is my code:

<messaging:emailTemplate subject=""
    recipientType="" 
    replyTo=""
    relatedToType="">

<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>Below are Components and Sub-Components</p>
        <table border="0">
            <tr>
                <th>x</th>
                <th>x</th>
                <th>x</th>
                <th>x</th>
                <th>x</th>
                <th>x</th>
            </tr>
            <apex:repeat var="component" value="{!relatedTo.SGI_Components__r}">
                <apex:outputPanel layout="none" rendered="{!component.Component_Status__c != 'Open'}">
                    <tr>
                        <td>{!component.Name}</td>
                        <td>{!component.Part_Number__c}</td>
                        <td>{!component.Total_Part_Authorized__c}</td>
                        <td>{!component.Labor_Amount__c}</td>
                        <td>{!component.Total_Amount__c}</td>
                        <td>{!component.Component_Status__c}</td>
                    </tr>
                    <apex:variable var="subComponents" value="{!component.SGI_Claim_Subcomponent__r}" />
                    <apex:outputPanel layout="none" rendered="{!NOT(ISBLANK(subComponents))}">
                        <tr>
                            <td colspan="6">
                                <table>
                                    <tr>
                                        <th>Sub-Component Name</th>
                                        <th>Sub-Component Part Number</th>
                                        <th>Sub-Component Total Authorized</th>
                                        <th>Sub-Component Status</th>
                                    </tr>
                                    <apex:repeat var="subComponent" value="{!subComponents}">
                                        <tr>
                                            <td>{!subComponent.Name}</td>
                                            <td>{!subComponent.Part_Number__c}</td>
                                            <td>{!subComponent.Total_Authorized_Amount__c}</td>
                                            <td>{!subComponent.Subcomponent_Status__c}</td>
                                        </tr>
                                    </apex:repeat>
                                </table>
                            </td>
                        </tr>
                    </apex:outputPanel>
                </apex:outputPanel>
            </apex:repeat>
        </table>
Best Answer chosen by ahon
ahonahon
Requirement:
Main Component #1
Sub #1
Sub #2
Main Component #2
Sub #1
Sub #2
 
Here is my code for a visualforce classic email template:

<messaging:emailTemplate subject="Claim Coversheet"
    recipientType="Contact" 
    replyTo="aoneill@sgintl.com"
    relatedToType="SGI_Claim__c">

<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>Below are Claim # {!relatedto.Claim_Id__c} Components and Sub-Components</p>
        <table border="0">
            <tr>
                <th>Name</th>
                <th>Part Number</th>
                <th>Total Authorized</th>
                <th>Labor Total</th>
                <th>Total Amount</th>
                <th>Component Status</th>
            </tr>
            <apex:repeat var="component" value="{!relatedTo.SGI_Components__r}">
            <apex:outputPanel layout="none" rendered="{!component.Component_Status__c != 'Open'}">
                    <tr>
                        <td>{!component.Name}</td>
                        <td>{!component.Part_Number__c}</td>
                        <td>{!component.Total_Part_Authorized__c}</td>
                        <td>{!component.Labor_Amount__c}</td>
                        <td>{!component.Total_Amount__c}</td>
                        <td>{!component.Component_Status__c}</td>
                    </tr>
 <apex:variable var="subComponents" value="{!component.SGI_Claim_Subcomponent__r}" />
                    <apex:outputPanel layout="none" >
                        <tr>
                            <td colspan="6">
                                <table>
                                    <tr>
                                        <th>Sub-Component Name</th>
                    <th>Part Number</th>
                    <th>Total Authorized</th>
                    <th>Status</th>
                                    </tr>
                                    <apex:repeat var="subComponent" value="{!subComponents}">
                                        <tr>
                        <td>{!subComponent.Name}</td>
                        <td>{!subComponent.Part_Number__c}</td>
                        <td>{!subComponent.Total_Authorized_Amount__c}</td>
                        <td>{!subComponent.Subcomponent_Status__c}</td>
                                        </tr>
                                    </apex:repeat>
                                </table>
                            </td>
                        </tr>
                    </apex:outputPanel>
                </apex:outputPanel>
            </apex:repeat>
        </table>
<br/>     
<br/>
<p>
      Sincerely,<br/></p>
      x<br/>
      Return Email: x<br/>
<br/>
<font color="red">
x
</font>
<br/>
<br/>
<font color="red">
v6.0
</font>
<br/>
</font>
 </body>
    </html>

All Answers

SubratSubrat (Salesforce Developers) 
Hello Ahon ,

To display two related lists of an object on an email template with nested related lists, you can use Visualforce components such as <apex:repeat> and <apex:outputPanel> to iterate over the related lists and conditionally render the nested related lists

Please try with the below code:
<messaging:emailTemplate subject="" recipientType="" replyTo="" relatedToType="">
    <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>Below are Components and Sub-Components</p>
                    <table border="0">
                        <tr>
                            <th>x</th>
                            <th>x</th>
                            <th>x</th>
                            <th>x</th>
                            <th>x</th>
                            <th>x</th>
                        </tr>
                        <apex:repeat var="component" value="{!relatedTo.SGI_Components__r}">
                            <apex:outputPanel layout="none" rendered="{!component.Component_Status__c != 'Open'}">
                                <tr>
                                    <td>{!component.Name}</td>
                                    <td>{!component.Part_Number__c}</td>
                                    <td>{!component.Total_Part_Authorized__c}</td>
                                    <td>{!component.Labor_Amount__c}</td>
                                    <td>{!component.Total_Amount__c}</td>
                                    <td>{!component.Component_Status__c}</td>
                                </tr>
                                <apex:variable var="subComponents" value="{!component.SGI_Claim_Subcomponent__r}" />
                                <apex:outputPanel layout="none" rendered="{!NOT(ISBLANK(subComponents))}">
                                    <tr>
                                        <td colspan="6">
                                            <table>
                                                <tr>
                                                    <th>Sub-Component Name</th>
                                                    <th>Sub-Component Part Number</th>
                                                    <th>Sub-Component Total Authorized</th>
                                                    <th>Sub-Component Status</th>
                                                </tr>
                                                <apex:repeat var="subComponent" value="{!subComponents}">
                                                    <tr>
                                                        <td>{!subComponent.Name}</td>
                                                        <td>{!subComponent.Part_Number__c}</td>
                                                        <td>{!subComponent.Total_Authorized_Amount__c}</td>
                                                        <td>{!subComponent.Subcomponent_Status__c}</td>
                                                    </tr>
                                                </apex:repeat>
                                            </table>
                                        </td>
                                    </tr>
                                </apex:outputPanel>
                            </apex:outputPanel>
                        </apex:repeat>
                    </table>
                </font>
            </body>
        </html>
    </messaging:htmlEmailBody>
</messaging:emailTemplate>


Please ensure that you have replaced the placeholder "x" with the appropriate column headers for your related lists.

If this helps , please mark this as Best Answer.
Thank you.
ahonahon
Requirement:
Main Component #1
Sub #1
Sub #2
Main Component #2
Sub #1
Sub #2
 
Here is my code for a visualforce classic email template:

<messaging:emailTemplate subject="Claim Coversheet"
    recipientType="Contact" 
    replyTo="aoneill@sgintl.com"
    relatedToType="SGI_Claim__c">

<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>Below are Claim # {!relatedto.Claim_Id__c} Components and Sub-Components</p>
        <table border="0">
            <tr>
                <th>Name</th>
                <th>Part Number</th>
                <th>Total Authorized</th>
                <th>Labor Total</th>
                <th>Total Amount</th>
                <th>Component Status</th>
            </tr>
            <apex:repeat var="component" value="{!relatedTo.SGI_Components__r}">
            <apex:outputPanel layout="none" rendered="{!component.Component_Status__c != 'Open'}">
                    <tr>
                        <td>{!component.Name}</td>
                        <td>{!component.Part_Number__c}</td>
                        <td>{!component.Total_Part_Authorized__c}</td>
                        <td>{!component.Labor_Amount__c}</td>
                        <td>{!component.Total_Amount__c}</td>
                        <td>{!component.Component_Status__c}</td>
                    </tr>
 <apex:variable var="subComponents" value="{!component.SGI_Claim_Subcomponent__r}" />
                    <apex:outputPanel layout="none" >
                        <tr>
                            <td colspan="6">
                                <table>
                                    <tr>
                                        <th>Sub-Component Name</th>
                    <th>Part Number</th>
                    <th>Total Authorized</th>
                    <th>Status</th>
                                    </tr>
                                    <apex:repeat var="subComponent" value="{!subComponents}">
                                        <tr>
                        <td>{!subComponent.Name}</td>
                        <td>{!subComponent.Part_Number__c}</td>
                        <td>{!subComponent.Total_Authorized_Amount__c}</td>
                        <td>{!subComponent.Subcomponent_Status__c}</td>
                                        </tr>
                                    </apex:repeat>
                                </table>
                            </td>
                        </tr>
                    </apex:outputPanel>
                </apex:outputPanel>
            </apex:repeat>
        </table>
<br/>     
<br/>
<p>
      Sincerely,<br/></p>
      x<br/>
      Return Email: x<br/>
<br/>
<font color="red">
x
</font>
<br/>
<br/>
<font color="red">
v6.0
</font>
<br/>
</font>
 </body>
    </html>
This was selected as the best answer