Wednesday 6 April 2016

How to add or access related object or related list record details in Visualforce email template in Salesforce

          Visualforce Email Template supports to access a related list or related object records by using relatedTo.reationshipObjectName.

In the below example, showed Contact and Opportunity records related the Account object.

Here,
Account --> Parent Object
Contact and Opportunity --> Child Objects

In email template,
Contact records accessed by relatedTo.Contacts
Opportunity records accessed by relatedTo.Opportunities


Sample VF Template:

  1. <messaging:emailTemplate subject="Your Detailed Contact and Opportunities" recipientType="User" relatedToType="Account">
  2.     <messaging:htmlEmailBody >
  3.         <p> Dear {!relatedTo.Name}</p>
  4.         The following customers are waiting for your approval,
  5.        <ul>
  6.             <apex:repeat var="cn" value="{!relatedTo.contacts}">
  7.                 <li>
  8.                      {!cn.Name}
  9.                 </li>
  10.             </apex:repeat>
  11.        </ul>
  12.        The following opportunities are ready to close,
  13.        <ul>
  14.             <apex:repeat var="cn" value="{!relatedTo.opportunities}">
  15.                 <li>
  16.                      {!cn.Name}
  17.                 </li>
  18.             </apex:repeat>
  19.        </ul>
  20.         <p> Thanks, </p>
  21.         <p> Sales Team </p>
  22.     </messaging:htmlEmailBody>
  23. </messaging:emailTemplate>

2 comments:

  1. Is it possible get grand child records to display in vf template?
    thanks.

    ReplyDelete

Activities: Assign Tasks to a Queue Salesforce Lightning

Salesforce announced to assign Tasks to a Queue beginning from Spring'20 release. How does it work? In Setup, enter Queues in th...