You need to sign in to do that
Don't have an account?
IMU 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
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
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.
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
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.
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
Your Visualforce Component will look something like
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.
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?
<a href="https://na1.salesforce.com/{!cx.id}">View</a> |
<a href="https://na1.salesforce.com/{!cx.id}/e">Edit</a>
Still getting the same error message I think this could be because this is a custom object??
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?
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