1

I am having one Email Template which I want to send in an email - How can i achieve that one -

String strToEmail=objEmp.Office_Email_c;
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
String []toAddresses = new String[]{strToEmail};
//String[] ccAddresses=new String[]{'nilesh@test.com'};
//String[] bccAddresses = new String[]{'nilesh@test.com'};

email.setHtmlBody('Dear '+objEmp.Name +',');
//Use existing Email Template
EmailTemplate objEmailTemplate=[select id, name from EmailTemplate where Name='Birthday Emailer'];
email.setTemplateId(objEmailTemplate.id); 
email.setToAddresses(toAddresses);
//email.setTargetObjectId();
//email.setCcAddresses(ccAddresses);// ????? CANT USED WHEN WE HAVE TEMPLATE
//email.setBccAddresses(bccAddresses); // ????? CANT USED WHEN WE HAVE TEMPLATE
Messaging.sendEmail(New Messaging.SingleEmailMessage[]{email});
2

Summer 16 released a very nifty method Messaging.renderStoredEmailTemplate(templateId, whoId, whatId) which returns a Messaging.SingleEmailMessage object and allows you to use setToAddresses() and other methods like you're used to. For the whoId, I just set it to UserInfo.getUserId() if I'm just referencing a custom object.

See this post for a quick overview of the new method.

Here's a sample helper method I like to use:

public static Messaging.SingleEmailMessage prepEmailTemplate(String[] toAddresses, Id templateId, Id whoId, Id relatedToId) {
    Messaging.SingleEmailMessage mail = Messaging.renderStoredEmailTemplate(templateId, whoId, relatedToId);
    mail.setBccSender(false);
    mail.setUseSignature(false);
    mail.setToAddresses(toAddresses);   // Finally! I can set toAddresses to email templates!
    mail.setReplyTo(ADMIN_REPLYTO_EMAIL);
    mail.setSenderDisplayName(ADMIN_SENDER_DISPLAYNAME);
    mail.setSaveAsActivity(false);      // Must be set to false, otherwise gives error when trying to sendEmail()
    return mail;
}
0

Seems you are using a custom email string with a template. This isn't accepted by Salesforce. You need to pass in a Contact object ID if you are going to use email templates.

Hope this helps. Peter

0

You can't use setToAddresses when a template is used as well. You should use setTargetObjectId to specify the ID of the contact, lead, or user to which the email will be sent. In addition, you can use setWhatId to provide data for merge fields in the template. Here's the documentation of Messaging.SingleEmailMessage.

Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setTemplateId([select Id from EmailTemplate where ...].Id);
email.setTargetObjectId([select Id from Contact where ...].Id);
email.setWhatId(objEmp.Id);
Messaging.sendEmail(new Messaging.SingleEmailMessage[]{email});
0

You can refer to this link http://developer.force.com/cookbook/recipe/creating-email-templates-and-automatically-sending-emails It uses the email template to send email from apex

0
setTargetObjectId(targetObjectId) 

Required if using a template, optional otherwise. The ID of the contact, lead, or user to which the email will be sent. The ID you specify sets the context and ensures that merge fields in the template contain the correct data.
Ref: SingleEmailMessage Methods

The problem is that if you specify a TargetObjectId (the record from which the Email Template will probably draw most of its merge fields), Apex’s Messaging classes require you to specify a WhatID that refers to a Contact, not a User. But what if you don’t want to send your email to a Contact? What if you want to use an Email Template with a TargetObjectId to send an email to a User? Or to an email address that isn’t associated with a Contact or a User? Are you out of luck?

With a little magical Apex hand-waving, you can indeed send your email just the way you want to.

Consider this code:
1 // Pick a dummy Contact
2 Contact c = [select id, Email from Contact where email <> null limit 1];
3
4 // Construct the list of emails we want to send
5 List<Messaging.SingleEmailMessage> lstMsgs = new List<Messaging.SingleEmailMessage>();
6
7 Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();
8 msg.setTemplateId( [select id from EmailTemplate where DeveloperName='My_Email_Template'].id );
9 msg.setWhatId( [select id from Account limit 1].id );
10 msg.setTargetObjectId(c.id);
11 msg.setToAddresses(new List<String>{'random_address@test.com'});
12
13 lstMsgs.add(msg);
14
15 // Send the emails in a transaction, then roll it back
16 Savepoint sp = Database.setSavepoint();
17 Messaging.sendEmail(lstMsgs);
18 Database.rollback(sp);
19
20 // For each SingleEmailMessage that was just populated by the sendEmail() method, copy its
21 // contents to a new SingleEmailMessage. Then send those new messages.
22 List<Messaging.SingleEmailMessage> lstMsgsToSend = new List<Messaging.SingleEmailMessage>();
23 for (Messaging.SingleEmailMessage email : lstMsgs) {
24 Messaging.SingleEmailMessage emailToSend = new Messaging.SingleEmailMessage();
25 emailToSend.setToAddresses(email.getToAddresses());
26 emailToSend.setPlainTextBody(email.getPlainTextBody());
27 emailToSend.setHTMLBody(email.getHTMLBody());
28 emailToSend.setSubject(email.getSubject());
29 lstMsgsToSend.add(emailToSend);
30 }
31 Messaging.sendEmail(lstMsgsToSend);

In lines 1-14, we begin by constructing an email message using a WhatID that refers to some random Contact. As long as our email template doesn’t use “recipient” merge fields, it doesn’t matter which Contact we pick: we’re never going to actually send the email message to that Contact.

In lines 16-18, we set a savepoint, send the email, and then roll back the transaction. The key here is to understand that Salesforce doesn’t send an email immediately when the sendEmail method is executed. Instead, Salesforce waits until the very end of the transaction. If you roll back the transaction, Salesforce doesn’t send the email at all.

If sendEmail doesn’t actually send the email message, why bother calling it? Because when you call sendEmail, Salesforce populates the email message’s Subject, Body, PlainTextBody, and HTMLBody based on the Email Template, TargetObjectId, and What ID. When you roll back the transaction, Salesforce retains the populated values in the Subject, Body, and other fields. So after you roll back the transaction, you can construct a new SingleEmailMessage, and copy the Subject, Body, and other values from the populated message into the new message. Your new message is able to leverage the work that the email template did, but because your new message doesn’t refer to the email template, you don’t have to provide the emailTemplateId, TargetObjectId or WhatID, and you’re no longer bound by the requirement to specify a Contact for the WhatID. You can send your email to whomever you want, leveraging a Subject and Body that were created by an Email Template.

Ref : Sending Emails in Salesforce to Non-Contacts Using Apex

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.