When I am doing a test send using the data in the quote object record, I am getting an html formatted email with values.

In the doc: https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_email_outbound_single.htm

It says:

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.

setWhatId(whatId) If you specify a contact for the targetObjectId field, you can specify an optional whatId as well. This helps to further ensure that merge fields in the template contain the correct data.

Not sure what values should be set for these:



Merge fields in my classic email template are like: {!Quote.Opportunity_Amount__c}

I am not sure I the SELECT should be like instead:

EmailTemplate et = [SELECT Id, Subject, Description, HtmlValue, DeveloperName, Body FROM EmailTemplate WHERE Name = 'My template name'];

Following is the code snippet I am using:

for (Quote q: Qt){
     EmailTemplate et = [SELECT Id, Subject, Body FROM EmailTemplate WHERE DeveloperName =:emailTemplateName];
                List<string> toAddress = new List<string>();
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                List<Messaging.SingleEmailMessage> allmsg = new List<Messaging.SingleEmailMessage>();

                try {
                } catch (Exception e) {
  • By setting the template ID this automatically identifies the subject and body to use. Simply do not set those values and allow Salesforce to sort those out for you. The who ID is the ID of the recipient record - always use this instead of to email address if possible since email address sending always uses limits- and the what ID is there to let you substitute values into the email template.
    – Phil W
    Oct 20, 2021 at 6:22
  • Right. I updated the code with mail.setTargetObjectId(q.Id); and removed whatid. But email doesn't seem to get sent out. Checked logs, recipient email is there... subject, etc is there... tried with two email ids...
    – Col
    Oct 20, 2021 at 8:10
  • 1
    The target is the recipient - the person to receive the email. A quote is not a person. Where you want to include details from the quote in the email, set the quote's ID as the what ID.
    – Phil W
    Oct 20, 2021 at 8:54
  • 1
    Clearly it is. Follow the constraints defined in the documentation and find a user, contact, lead or person ID to pass as the target ID.
    – Phil W
    Oct 20, 2021 at 9:48
  • 1
    Read what I said: DO NOT SET THE BODY AND SUBJECT. By setting the template ID you are telling Salesforce to go query the template body and subject for itself, and to apply mailmerge handling all in one go. If you are going to query them yourself you a) must call the required API to handle the mailmerge processing and b) should not set the template ID.
    – Phil W
    Oct 20, 2021 at 14:32

1 Answer 1


If you are using an email template, want to send an email using that template (and can afford for Salesforce to perform a query to load that template) and have mailmerge in the template, the easiest way to achieve this is to:

  1. Call setTemplateId with the ID of the required email template. Do not separately call setSubject, setHTMLBody or setPlainTextBody; the template will be automatically queried for you and mailmerge will be processed for you. This mailmerge processing won't happen if you set subject/body explicitly.
  2. Call setTargetObjectId passing in the ID of the recipient. Note that this must be a User, Contact, Lead or Person record ID. Also note that if it is not a User or Lead ID then the email send counts against your daily email sending limit.
  3. Call setWhatId passing in the ID of the record referenced in the template's mailmerge.

If at all possible, always try to map a Contact ID to a User ID (in case you have Community Users) before using as the recipient of the email; that way you don't eat into your email daily limits.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .