Using Basic Email templates from Salesforce Apex Code

Using Basic Email Templates From Salesforce Apex
Sharing is Caring

Sending Emails from Salesforce is pretty easily implemented using Salesforce and the Email Templates. I strongly recommend using Email Templates over visualforce emails and hardcoding text emails within the source code because making changes can be really time consuming otherwise. Of course, there are times when it makes sense to use visualforce emails or hardcoding text emails but these exceptions should not be the norm.

By using an html email template, it’s now possible for a Salesforce Admin or for somebody in marketing to be able to update the email without having to wait for developers to make the needed changes. If your company is using Pardot or other marketing automation software it would probably make a lot more sense to send the email through it. If you’re not sure what marketing automation is, I’ve written a post called What is Marketing Automation.

Email Sending Limits In Salesforce

One thing to keep in mind is that Salesforce has a limit for the number of emails that can be sent in a 24 hour period. I believe it’s currently a hard limit of 5,000 emails which isn’t really significant if your company has a hundred users or so. There’s also a limit of 10 email sends per transaction which is probably fine as this isn’t really meant to be sending email newsletters or anything like that, it’s more of a transactional sort of email.

A common use case that comes to my mind is sending an email when your sales rep hasn’t been able to talk to the Lead or a Contact because they didn’t answer their email. In this particular example, you would most likely have a button on the page layout that would say something like “Send No Answer Email” which could automatically send an email when pressed.

How To Send Emails Using Apex

Emails can be sent using the Messaging class which is pretty well documented. The majority of the work is actually done in the Messaging.SingleEmailMessage class through the dozen or so methods and properties.

For the most part, I recommend only using the setTemplateId() and and setTargetObjectId() fields which can handling doing mail merges from the template without having to do any real manipulation. To take advantage of the setTemplateId() method we need to query for the HTML Email Template which is pretty easy to do. When querying for the email template, make sure that you are using the Unique Name to query because it’s much more unlikely that the unique name would be changed.

Code to send an email would look something like this:

// get a contact - wouldn't normally do it like this
Contact con = (Contact)[select Id from Contact Where Name = 'Brian Cline' Limit 1]

// get an email template
EmailTemplate tpl = (EmailTemplate)[select Id FROM EmailTemplate WHERE Name = 'My Email Name' limit 1]

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTemplateId(tpl.Id);
mail.setTargetObjectId(con.Id)

//send the email
List<Messaging.SendEmailResult> results = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

// we can then check for success
if (results[0].success) {
showASuccessMessage();
} else {
showFailure();
}

This quick post should help you to understand how Emails can be sent really easily from within Salesforce. Keep in mind that Messaging.sendEmail() can do bulk operations.

Author: Brian Cline

Brian is a programmer living in Niagara Falls with ten years of development experience. He is passionate about automation, business process re-engineering, and gamification. For the last six years or so, he has been working with Salesforce and JavaScript.

Related Posts

Sharing is Caring