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
PRepakaPRepaka 

how to pass values to merge fields in Email templates from apex class

hi
 
i created a new Email Template of type Custom or Visualforce or Text.
 
The following case has been assigned to you.
Company: {!Account.Name}
Contact Name: {!Contact.Name}
Case #: {!Case.CaseNumber}
Subject: {!Case.Subject}
Description: {!Case.Description} 
 
i write an apex class for calling this email template and sending an email.
 
i am sending an email to the User.
 
But i dont know how to pass values to Merge fields(ex: {!Account.Name}) in that email template from an apex class. 
 
How can i pass values to merge fields in Email template.


Message Edited by PRepaka on 11-25-2008 05:00 AM
beener3beener3
I'm having the same problem. did you get this sorted out?

Thanks

Ben
DrawloopSupportDrawloopSupport

I also need an answer to this question. Can anyone help us out?

 

Thanks.

Suresh KalluriSuresh Kalluri

hi,

i am also facing the same issue...Can any one suggest a way..!!

trmptrmp

I believe if the email is being sent from Apex, the only way to do this is to specify the "Who Id" and "What Id". Salesforce will populate the {!fields} based on the records specified in those fields. In the Email Message Methods in Apex, use the setWhatId and setWhoId methods.

ForceRohitForceRohit

I need the solution for the same question . is there any one who got the solution or else the way for the solution please do post it

trmptrmp

Rohit,

 

Please see my previous post. I believe the only way to do this is to set the whoId to a contact Id and the whatId to another object that has activities enabled. You might also be able to try Visualforce templates, but you'd have to look up the documentation for that.

Nechama Kremer 10Nechama Kremer 10
Hi,
You can use String.replace() to insert values to your email template.
for example:

EmailTemplate emailTemplate = [SELECT Id, body, HtmlValue, Subject 
                                                     FROM EmailTemplate 
                                                     WHERE DeveloperName = 'email template name....' LIMIT 1];

Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
String body = emailTemplate.HtmlValue;
body = body.replace('{!Account.Name}', 'value.....');
body = body.replace('{!Contact.Name}', 'value....');
email.setHtmlBody(body);
email.setTemplateId(emailTemplate.Id);
email.setSaveAsActivity(false);
email.setTargetObjectId(users[0].Id);
            
List<String> usersEmails= new List<String>{'email@gmail.com','xxx@gmail.com'}; //list of emails to sent to
email.setToAddresses(usersEmails);
            
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email});
 
sfdcpandasfdcpanda
Hi Nechama,
I know it is a old thread but I need some help on this
If we are sending the mail using this replace functionality, there is a chance of getting Special Characters at the end of email template becoz we are converting the template to HTML format... will please help me out how to resolve this issue..

Thanks in Advance.........