Using Merge Fields in Salesforce Email Templates
Learn how to integrate dynamic content in a Salesforce email template and how to reference values from a lookup relationship
One of the neat functionalities in Salesforce is the ability to send an email through an automated process such as workflow rules, process builder, or even Apex code. You can even personalize the email using dynamic content called merge fields. For example, we can add a person’s first name, order number, application number, or other relevant information pertaining to the email. In this lesson we will cover how to use merge fields in an email template. We’ll also cover the scenario of how to retrieve information from a lookup relationship.
First, let’s go over the objects we’ll be using. There will be 2 objects. One will be a standard object called the Contact. While the other will be a custom object called Application. The Application object will have a lookup relationship to the Contact object.
The name of this custom object is exactly what it implies. The Application object will consist of applicant information and can represent anything such as applying to graduate school, applying for a loan, or applying for a job. The object will be a one to many relationship to the Contact object meaning a contact record can have multiple applications. Here is a screenshot of the fields that will be used in this object which is a very basic setup:
Now let’s cover our first example.
Creating the email template
We’ll first login to Salesforce and proceed to creating our email template. Click on the Setup option which is located on the top right screen of Salesforce. You will then see a list of collapsible items available to the left of the screen. There you will find a collapsible item called Communication Templates. Expand that item and you should see an item called Email Templates. You can also get to Email Templates by typing it in the Quick Find / Search bar located at the top of the list of collapsible items. Searching in the quick find box will filter the collapsible items on the same screen.
In the Email Templates screen, there will be a button called New Template. Select that option and we have 4 options to choose from on the next screen: Text, HTML, Custom, or Visualforce. Ideally, we would want to use the HTML or Custom template so that we can design the appearance of our emails, but for this lesson we will select the Text email template since our focus is to cover the topic of merge fields.
Now let’s create a basic email template. We will save this email template in the default folder, Unfiled Public Email Templates, and the contents of our email body will look something like this:
Dear {!Contact.FirstName},
Welcome to our email list.
Here's the information we have about you. Please let us know if this is incorrect.
Full Name: {!Contact.Name}
Home Phone: {!Contact.HomePhone}
Mobile Phone: {!Contact.MobilePhone}
Email: {!Contact.Email}
Thanks again and welcome!
This basic template only refers to the Contact object. The merge field consist of the object name and the field name which is then enclosed by the following characters, {!}
. The value of this structure, {!ObjectApiName.fieldApiName}
, is then retrieved dynamically. You also may have noticed that the text email template screen also includes a section to help you construct your merge fields. Here is a screenshot of what the merge builder looks like when creating a template.
However, the merge field builder usually looks like this for the other email templates and when you edit an existing text email template.
Now let’s create another email template using the Application and Contact object.
Retrieving merge field values from multiple objects
Hello {!Contact.FirstName},
Thank you for submitting your application to the following program, {!Application__c.Program__c}
on the following date: {!Application__c.Submit_Date__c}. We'll send you updates on your application
after we have reviewed it.
Thank you!
However, if we test this template the {!Contact.FirstName}
field will return a blank value in the email. This is because a merge field in the email template is dependent on the object value of the
Email Alert
. Email alerts are emails generated by an automated process and this is where the email template is referenced. When creating the alert, there is an option of setting the object which is required. This value is then used as a reference when generating the merge field values for the email template. This is why the value is blank because we will be using the Application object for our Email Alert. Therefore, it cannot traverse to the Contact object by directly referencing the contact field.
Now, you might be thinking that we can traverse to the First Name field using the child to parent relationship the Application object has with the Contact object. Perhaps we can replace the field, {!Contact.FirstName}
, with {!Applicant__r.firstname}
. Unfortunately, this cannot be done. This is also not possible in the HTML or Custom email templates. It can however be done in Visualforce, but it is preferable to allow end users to edit the text of the template at any time in the first three templates. Otherwise, a developer or administrator will most likely need to edit the Visualforce template. This issue though, has been recognized by the Salesforce team and acknowledges the need for this functionality. Please see the following IdeaExchange link for more details:
Cross Object Merge Fields in Email Template
However, we can still workaround this issue. What we’ll need to do is create a custom text formula field on the Application object that references the First Name
field, Applicant__r.firstname
. We can call this field, Applicant First Name and the API Name will be called Applicant_First_Name__c. Replacing {!Contact.FirstName}
with our new formula field should now give us the first name of our applicant.
Utilizing merge fields in an email template is a great feature so that you can provide that personalized look without sacrificing the need to send mass emails or send through an automated process. However, keep in mind of the limitation in text, HTML, and custom type email templates. If you want to pull relevant information from a lookup object, you’ll need to create a formula field for each field that you’ll want to use from that object. Nevertheless, merge fields are still useful in email templates which can simplify the business needs for anyone.
Share this post
Twitter
Google+
Facebook
Reddit
LinkedIn
StumbleUpon
Email