Print this page

Using RelatedTo fields in VF email template

Knowledge Article Number 000176935
Description How to access the child fields in VF Email Template from an object and then using the Email template to send email.



 
Resolution Resolution using this sample code:

VF template sample code: 

******************************************************************************************************************************** 
<messaging:emailTemplate subject="Testing Related TO" recipientType="Lead" relatedToType="Lead"> 
<messaging:plainTextEmailBody > 
Congratulations! 
This is your new Visualforce Email Template. 
</messaging:plainTextEmailBody> 

<messaging:htmlEmailBody > 

<apex:repeat value="{!relatedTo.testEmailTemplate__r}" var="tx" > 

<td>{!tx.Name}</td> 
<td>{!tx.testSFDC__c}</td> 


</apex:repeat> 

</messaging:htmlEmailBody> 

</messaging:emailTemplate>


*********************************************************************************************************************************
Running this code from developer console can use this template to send the email:

 
Messaging.SingleEmailMessage mail = 
new Messaging.SingleEmailMessage(); 

// Who you are sending the email to 
mail.setTargetObjectId('00Qd000000HlRU6'<Recipient ID>); 

// The email template ID used for the email 
mail.setTemplateId('00Xd0000001Ig14'<template id>); 

mail.setWhatId('00Qd000000HlRU6'<relatedtotype id >); 
mail.setBccSender(false); 
mail.setUseSignature(false); 
//mail.setReplyTo('recruiting@acme.com'); 
mail.setSenderDisplayName('HR Recruiting'); 
mail.setSaveAsActivity(false); 

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });


 




promote demote