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
Shruthi GM 4Shruthi GM 4 

how to pass values from apex class to email Template?

I have captured the values.Now that I have to send those values to the email template.How to do that?
Please help.
Rupal KumarRupal Kumar

public  class acctTemplt
    public Id accountId {get;set;}
    public List<Opportunity> getopptys()
        List<Opportunity> oppty;
        oppty = [SELECT Name, StageName FROM Opportunity WHERE Accountid =: accountId];
        return oppty;


<apex:component controller="acctTemplt" access="global">
    <apex:attribute name="AcctId" type="Id" description="Id of the account" assignTo="{!accountId}"/>
    <table border = "2" cellspacing = "5">
            <td>Opportunity Name</td>
            <td>Opportunity Stage</td>                
        <apex:repeat value="{!opptys}" var="o">

Visualforce Email template:
<messaging:emailTemplate subject="List of opportunity" recipientType="User" relatedToType="Account">
    <messaging:htmlEmailBody >
    Below is the list of opportunities for your account {!relatedTo.Name}.<br/><br/>
    <c:OpptyList AcctId="{!relatedTo.Id}" /><br/><br/>

User-added image

Thanks & regards
Rupal kumar

Sagar PareekSagar Pareek
Hi Shruthi,
How you will be sending email from salesforce? Using Apex?
Sagar PareekSagar Pareek
If yes then you can use following code -
public class EmailUtil // class that you will use to send email 
    public static void mailSender(List<Id> LeadList)
        EmailTemplate emailTemplate = [select Id, Subject, HtmlValue, Body from EmailTemplate where Id ='00Xp0000000Dcuv']; // or use email template name instead
        for(lead l:[select id,name,DripFromEmail__c,Owner.Name,Owner.Email from lead where Id IN:LeadList]){
            String subject = emailTemplate.Subject;

            String htmlBody = emailTemplate.HtmlValue;
            htmlBody = htmlBody.replace('{!Lead.Name}', l.Name);
            htmlBody = htmlBody.replace('{!Lead.Name}', l.Name);
            htmlBody = htmlBody.replace('{!Lead.OwnerFullName}', l.Owner.Name);
            htmlBody = htmlBody.replace('{!Lead.OwnerEmail}', l.Owner.Email);
            String plainBody = emailTemplate.Body;
            plainBody = plainBody.replace('{!Lead.Name}', l.Name);
            plainBody = plainBody.replace('{!Lead.OwnerFullName}', l.Owner.Name);
            plainBody = plainBody.replace('{!Lead.OwnerEmail}', l.Owner.Email);
                //build the email message
            Messaging.Singleemailmessage email = new Messaging.Singleemailmessage();
            Messaging.sendEmail(new Messaging.SingleEmailmessage[] {email});

Shruthi GM 4Shruthi GM 4
Hi Sagar,
I have used in the similar manner,but am unable to fetch those values in the template?
what could be the reason?
Shruthi GM 4Shruthi GM 4
I am facing "System.NullPointerException: Attempt to de-reference a null object" near  plainBody = plainBody.replace('{!Lead.Name}', l.Name);
in my class.....
Shruthi GM 4Shruthi GM 4
Please let me know if you have any solutions for this.