Time and time again the issue of trying to send an email to a Salesforce User via apex using an Email Template has been a thorn in my side. Before the Summer 16 release all approaches involved using a contact record and save points to extract the information. Well have no fear with Summer 16 came the introduction of a new method renderStoredEmailTemplate. This method made it possible to send the emails without the need to use a contact on the fly or depend on database rollbacks.
You may be asking yourself, “Why is she writing about this feature now?” With the introduction of this new feature there was no clear documentation on how to implement the method, so in this post I will describe and provide code so that you to can send emails quickly and more efficiently to a Salesforce User.
Definitions
Code Explanation: The Nitty-Gritty
So in this example we will be working with the following assumptions:
- There is an existing Visualforce Email Template in the Org and the data we are trying to merge contains HTML elements
- For the whoId we are trying to send the email to internal Users.
- For the sake of simplicity the WhatId will be on Opportunity.
Messaging.SingleEmailMessage email = Messaging.renderStoredEmailTemplate(EmailTemplateId, UserId, OpportunityId);
This first line is crucial, when using the renderstoredEmailTemplate you need to return it to type: Message.SingleEmailMessage. It’s important to understand that this one single line merges fields and replaces the definitions with actual data based on the TemplateId, WhoId and WhatId. The email does not have to be enclosed in savepoints, like it’s mentioned here, one line that’s all it takes!
You have have made it this far give yourself a pat on the back and kick your feet up.
But wait, you might be worried that you’ve only coded one line and must be thinking what should I do next. Well lets continue.
String subject = email.getSubject(); String body = email.getHtmlBody();
Now that we have the email template with the correct data we need to extract it. To do this we use the getSubject() and getHtmlBody() methods and store the values in string variables. Since I am assuming that the email template has HTML elements I use the getHtmlBody() method to ensure that the correct formatting will be retained in the email.
All right super star, lets recap, you rendered your email template and you have now extracted the subject and body of said email template. Three lines in and your feeling amazing! Ready for the grand finale?
email.setTargetObjectId(UserId); email.setSubject(subject); email.setHtmlBody(body); email.saveAsActivity = false; //Send Email Now Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
Looks familiar, as it should, and now the rest should be a breeze. Using the methods mentioned above(setTargetObjectId, setSubject, setHtmlBody) you set the target object Id of the email, which in this case is a User Id so It will set the email address its going to, as well as the subject, and body. Because we setTargetObjectId as a User Id the value of saveAsActivity is set to false; since activities can not be saved for a User Record. Then all that’s left is to send the email!
Full Snippet : Longer Version
Messaging.SingleEmailMessage email = Messaging.renderStoredEmailTemplate(EmailTemplateId, UserId, OpportunityId); String subject = email.getSubject(); String body = email.gethtmlBody(); email.setTargetObjectId(UserId); email.setSubject(subject); email.sethtmlBody(body); email.saveAsActivity = false; //Send Email Now Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
Full Snippet : Shortened Version
Messaging.SingleEmailMessage email = Messaging.renderStoredEmailTemplate(EmailTemplateId, UserId, OpportunityId); email.setTargetObjectId(UserId); email.setSubject(email.getSubject()); email.sethtmlBody(email.gethtmlBody()); email.saveAsActivity = false; //Send Email Now Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});