+ Start a Discussion

Send Email using EmailTemplate with letterhead information

Please help me how to to send an email using EmailTemplate with letterhead information? Am I missing something here.
'targetObjectId' - sets the context and ensures that merge fields in the template contain the correct data.
'templateId'  - The ID of the template to be merged to create this email.

//create single email message

var request = new sforce.SingleEmailMessage();

//set id

request.targetObjectId = id;

//set email template id

request.templateId = emailTemplateId;

//send email

var result = sforce.connection.sendEmail([request]);

if (!result[0].getBoolean("success")) {



I'm also having this issue, however with bulk emails. There are a couple of points here:

1. If I am sending a template mail to a list of USERS, I cannot supply a list of Opportunities with which the template will populate itself.

2. There seems to be a way to do this if I was trying to send to CONTACTS, however I have to provide 2 arrays. First is a list of contacts and the second a same length array of opportunity IDs, where each contact gets only 1 email.

Ideally I would be able to provide an Array of USERS and a second "any length" array of Opportunity IDs, where every user gets 1 a mail about each opportunity that I have passed in.

Doesn't seem like this is possible right now, which means that using  an email template is not possible.

Anyone worked around this somehow?
Templates can't merge data from more than one record (e.g. opportunities), so you'd have to perform your query, obtain the data, and then send the email without using a Salesforce template. The body of the email would have to be populated in the script itself.
Seems unsatisfactory to have to have the html for an email in APEX code!!!! Especially if it is duplicating the template. I'd like to see some upgrades to the email functionality in APEX, seems quite basic at the moment.

I'm bumping this thread because 2 years later, this is still an issue. Simple use case:



  1. Opportunity is updated.
  2. Trigger on opportunity creates a SingleEmail object to send to the Opportunity.Account.OwnerId.
  3. Email template is predefined in configuration and template ID is passed into the single email object.
Failure: I am unable to merge in Opportunity fields using setWhatId(opportunity.Id) because the target object id is that of a user, forcing me instead to hardcode the outbound HTML right in the apex code. How does this limitation make sense, and why is it still an issue two years in?


So frustrating that this still doesn't seem to be possible.