9

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>();
    toAddress.add(primaryEmail);
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setTemplateId(et.Id);
        mail.setToAddresses(toAddress);
        mail.setSubject('test subject');
        mail.setTargetObjectId(primaryContact);
        mail.setWhatId(primaryAccount);
        mail.setSaveAsActivity(false);
        mail.setUseSignature(false);
    List<Messaging.SingleEmailMessage> allmsg = new List<Messaging.SingleEmailMessage>();
    allmsg.add(mail);
    
    try {
        Messaging.sendEmail(allmsg,false);
        return;
    } catch (Exception e) {
        System.debug(e.getMessage());
    }
    

Properties

  • 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.

Result

  • 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. – Avijit Chakraborty Jun 28 '17 at 15:34
  • 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? – Lucnex Jun 28 '17 at 16:49
  • This template is based on which object? – LetMeCodeYou Jun 28 '17 at 17:28
  • @AvijitChakraborty - looking in GMail, I'm referring to the "show original" I'm not seeing the HTML I expect. – fusion27 Jun 28 '17 at 17:31
  • @LetMeCodeYou I'm attempting to merge data against the Contact object. – fusion27 Jun 28 '17 at 17:33
5

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

mail.setWhatId(ContactId);

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.

1

You need to remove below 2 lines to solve empty email.

mail.setToAddresses(toAddress);

mail.setSubject('test subject');

once you provide the code with some part of mail(like subject, body) then it ignores the template.

  • Very helpful point!! – Dnyaneshwar Jan 4 '19 at 13:05
1

Replace the setSubject method parameter with the value you are fetching from the EmailTemplate object. This way you can change the email content by changing it in the template.

EmailTemplate et = [SELECT Id,Subject, Body FROM EmailTemplate WHERE DeveloperName =:emailTemplateName];
List<string> toAddress = new List<string>();
toAddress.add(primaryEmail);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    mail.setTemplateId(et.Id);
    mail.setToAddresses(toAddress);
    mail.setSubject(et.subject);
    mail.setHTMLBody(et.Body);
    mail.setTargetObjectId(primaryContact);
    mail.setWhatId(primaryAccount);
    mail.setSaveAsActivity(false);
    mail.setUseSignature(false);
List<Messaging.SingleEmailMessage> allmsg = new List<Messaging.SingleEmailMessage>();
allmsg.add(mail);

try {
    Messaging.sendEmail(allmsg,false);
    return;
} catch (Exception e) {
    System.debug(e.getMessage());
}

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.