If you’ve ever used EmailMessage to send emails via Apex, you have likely encountered a problem in setting up targetObjectId() if you were using an associated email template. This targetObjectId() must be a Contact, Lead, or a User record Id. Using a template is challenging when the email is not sent to a Contact, Lead, or User record but to a particular email address and the template does not have any merge fields specific to Contact or User or Lead.
Previously, there were two workarounds to overcoming this challenge:
- Create dummy contacts and then delete them when the email is sent.
- Set the message body in Apex itself, which can be cumbersome and time-consuming.
Another easier, recommended approach is to use renderStoredEmailTemplate. This method makes it possible to send emails without the need to use targetObjectId() or to depend on database rollbacks. It also eliminates the problem of setting whatId when targetObjectId is set to UserId.
Use Case: As soon as a Case is created via Email-to-Case, it is required to send an email to the address mentioned in the SuppliedEmail field. Note: Emails will be sent to the email address specified in the suppliedEmail field and not to any Contact, Lead, or User.
Code Explanation | Nuts and Bolts
Here we provide an explanation for the above use case in detail.
Line 1: The method takes two parameters:
- caseList: This is the list of cases which will be considered for this piece of code.
- emailTemplate: The name of the required template.
Line 4 - 19: We construct an email message using template ID, the email address to which the email will be sent and Case ID. When using the renderStoredEmailTemplate, we need to return it to type : Message.SingleEmailMessage.
Messaging.SingleEmailMessage email = Messaging.renderStoredEmailTemplate(templateId, whoId, whatId)
This one line merges fields and replaces the email content with actual data based on the following:
TemplateId: The ID of the emailTemplate that’s used to send emails via APEX.
WhoId: This is the ID of the Contact, Lead, or User to whom the email will be sent.
WhatId: The object of which the merge fields will be referenced.
Here, we are sending emails to addresses in the toAddressList. Therefore, WhoId is kept as null.
Now, all that’s left is to send the email!
Line 22 - 30: In the above lines, we have constructed the email message and stored it in emailMsgList. Although outbound email limit is 1000 with Apex, only a total of 10 sendEmail method calls are allowed in each context. Therefore, we have gathered all the emails in the emailMsgList and will now call the sendEmail method only once.
This makes the email functionality more robust and allows us to build out-of-the-box functionality for the clients.
We love to make it easier and faster for our customers and thus, choose the world’s best CRM to do that. Our team of Salesforce consultants and developers are experts in building various solutions using multiple clouds that transform your day-to-day business tasks. Get in touch with us to build a true partnership and implement the best CRM across your organization.
About the AuthorFollow on Linkedin More Content by Japleen Kaur