How to send an Email Template via Apex?

  • Goal

    • I'm attempting send a good looking HTML from Apex code, here's a stripped back screenshot Screenshot.
    • Based on the documentation I've reviewed, with Apex we can leverage SalesForce email templates using merge fields. I hope I'm understanding this correctly?
    • In this case I want to merge in Contact.FirstName.
    • I'm using the below code.

      EmailTemplate et = [SELECT Id FROM EmailTemplate WHERE DeveloperName =:emailTemplateName];
      List<string> toAddress = new List<string>();
      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
          mail.setSubject('test subject');
      List<Messaging.SingleEmailMessage> allmsg = new List<Messaging.SingleEmailMessage>();
      try {
      } catch (Exception e) {


    • mail.setTemplateId(et.Id); - I'm finding my email template in SalesForce. The template's "Available for Use" checkbox is thrown
    • mail.setToAddresses(toAddress); - I'm receiving the email at the address I'm sending to. I'm confident this is correct.
    • mail.setSubject('test subject'); - this is correct
    • mail.setTargetObjectId(primaryContact); - the recipitient's Contact.Id.
    • mail.setWhatId(primaryAccount); - the recipient's Account.Id.


    • I'm successfully receiving the email, subject is coming across as I expect, but the body is empty.
    • In SaleForce > Email Tematples, when I click the Send Test and Verify Merge Fields button, it works as expected, email looks great and that Contact.FirstName merges.

    Moving Forward

    I'm going to keep poking and hopefully answer my own question, but wondered if anyone has tackled this and gotten back a good looking HTML email with merged SalesForce data?

    Where you are trying to view this email. In web browser or in an email client. I have experienced if you have CDATA in your template it comes as blank when checking with the email client. You can see it in web browser though.

    Have you tried debugging this by running just that subset of code in the Execute Anonymous Window? Try setting the TargetObjectId to the Contact ID of the same Contact you are generating the merge fields from. I have done exactly what you are describing above, and it has worked for me by using that TargetObjectId field. I have not used the WhatID method though, but I assume it would be set to the Account that contact is related to?

    This template is based on which object?

    @AvijitChakraborty - looking in GMail, I'm referring to the "show original" I'm not seeing the HTML I expect.

    @LetMeCodeYou I'm attempting to merge data against the Contact object.

    Then you need to do this `mail.setWhatId(thisContact.Id)` instead of `Account.Id`.

    Hi what you see in the gmail by doing the view source (right click -> view page source). Can you share the HTML version of the template? Try also looking at basic HTML as well as other email services like Yahoo.

  • In your code you need to bring in a slight change. Where you setting AccountId in setWhatId() method, change it to


    A brief explanation from the docs.

    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.

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM