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
IMU AdminIMU Admin 

Add related list from parent record into child record VF email template

Hi,

I am trying to add a table with the related child records into a visual force email template.

The email template is is on the child object so I need the table to show all the related child records attached to the parent record. 

Both are custom objects does anybody know if this is possible and if so please assist
 

Thanks
 

Josh
logontokartiklogontokartik
Hi Josh,
You can write an apex controller and write your logic there to get any records you want and add it as a controller to your Visualforce Component, which you can use as an Email Template.
This way you can literally query anything in the Apex Controller.

 
IMU AdminIMU Admin
Hi,

Thank you for your response I am still pretty new to Apex so could you possible help with some example code. 

The Parent Object is Customer and the Child object is Invoice. 

So I am sending the email on the Invoice but need a table at the bottom to show all other Invoices on this Customer

I hope that makes sense

Thanks in advance
Gyanender SinghGyanender Singh
Hi IMU Admin,

Please use below code and in this account and for the child record i used case object so by this your requirement has been completed.


<messaging:emailTemplate recipientType="Contact" relatedToType="Account" subject="Case report for Account: {!relatedTo.name}" replyTo="ergyanendersingh@gmail.com">
  <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>Dear {!recipient.name},</p>
          <p>Below is a list of cases related to the account: {!relatedTo.name}.</p>
          <table border="1" cellpadding="0" cellspacing="0">
            <tr > 
               <th>Action</th>
               <th>Case Number</th>
               <th>Subject</th>
               <th>Creator Email</th>
               <th>Status</th>
            </tr>
            <apex:repeat var="cx" value="{!relatedTo.Cases}">
              <tr>
                <td><a href="https://na1.salesforce.com/{!cx.id}">View</a> |  
                <a href="https://na1.salesforce.com/{!cx.id}/e">Edit</a></td>
                <td>{!cx.CaseNumber}</td>
                <td>{!cx.Subject}</td>
                <td>{!cx.Contact.email}</td>
                <td>{!cx.Status}</td>
              </tr>
            </apex:repeat>                 
          </table>
          <p />
        </font>
      </body>
    </html>
  </messaging:htmlEmailBody> 
  <messaging:plainTextEmailBody >
    Dear {!recipient.name},
    
    Below is a list of cases related to Account: {!relatedTo.name}
    
    [ Case Number ] - [ Subject ] - [ Email ] - [ Status ]
    
    <apex:repeat var="cx" value="{!relatedTo.Cases}">
      [ {!cx.CaseNumber} ] - [ {!cx.Subject} ] - [ {!cx.Contact.email} ] - [ {!cx.Status} ]
    </apex:repeat>
    
    
  </messaging:plainTextEmailBody>    
</messaging:emailTemplate>

Thanks Gyani
please don't forget to mark as the best answer.
 
logontokartiklogontokartik
You can follow the same pattern as in the Developer guide

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

In your case your controller will be InvoicesForCustomer
 
public class InvoicesForCustomer {

 
	private final List<Invoice__c> allInvoices;
        private Invoice__c currentInvoice;

	public InvoicesForCustomer() {
		
	 }

	public List<Account> getAllInvoices() {
                allInvoices = [Select Id, Name from Invoice__c where Customer__c = :currentInvoice.Customer__c];
		return allInvoices;
	}

     public Invoice__c getcurrentInvoice {        
             return currentInvoice;
     }
     public void setcurrentInvoice(Invoice__c val) {
            this.currentInvoice = val;      
      }
}

Your Visualforce Component will look something like
 
<apex:component controller="InvoicesForCustomer" access="global">
	<apex:dataTable value="{!allInvoices}" var="inv">
		<apex:column>
			<apex:facet name="header">Invoice Name</apex:facet>
			{!inv.Name}
		</apex:column>
	</apex:dataTable>
</apex:component>
And you can use this in your Email Template.

Hope this helps

PS: the code I wrote is on the fly and didnt test it, so there might be typos or compile errors.


 
IMU AdminIMU Admin
Hi @Gyanender Singh

I have tried your code but get error as Unknown property 'String.id'. 

Both Parent and Child are custom objects.

The master detail field is called Customer_Name__c 

Here is the code I have 

   <table border="1" cellpadding="0" cellspacing="0">
            <tr > 
               <th>Action</th>
               <th>Case Number</th>
               <th>Subject</th>
               <th>Creator Email</th>
               <th>Status</th>
            </tr>
            <apex:repeat var="cx" value="{!relatedTo.Customer_Name__c}">
              <tr>
                <td><a href="https://na1.salesforce.com/{!cx.id}">View</a> |  
                <a href="https://na1.salesforce.com/{!cx.id}/e">Edit</a></td>
                <td>{!cx.CaseNumber}</td>
                <td>{!cx.Subject}</td>
                <td>{!cx.Contact.email}</td>
                <td>{!cx.Status}</td>
              </tr>
            </apex:repeat>                 
          </table>

Any ideas? 
 
Gyanender SinghGyanender Singh
Just replace the these url from your org url:
<a href="https://na1.salesforce.com/{!cx.id}">View</a> |  
<a href="https://na1.salesforce.com/{!cx.id}/e">Edit</a>
IMU AdminIMU Admin
Hi @Gyanender Singh

Still getting the same error message I think this could be because this is a custom object??
IMU AdminIMU Admin
Hi @logontokartik

I am getting an error on Line 17 unexpected token: 'return' - everything else seems to be working as no other issues.

Any ideas what could be causing this? 
logontokartiklogontokartik
yeah sorry that should be a method getcurrentInvoice()
IMU AdminIMU Admin
@logontokartik

I managed to resolve this issue however I am now getting below message on my Visual Force Template:


Error occurred trying to load the template for preview: Attempt to de-reference a null object. Please try editing your markup to correct the problem.

Really appreciate your support