Declarative

How to add cross-object fields as merge fields in Lightning Email Template

Lightning Email Templates for sure are superior to their old counterpart, Classic Email Templates, however, they too come with certain limitations – like every other thing in Salesforce. In Lightning Email Templates, you can easily add the fields of a single object as merge fields. These merge fields are then replaced with the values stored in the individual record utilized in the email-sending process. The limitation is that we can’t use cross-object fields as merge fields in Lightning Email Templates.

At the moment of this writing, there is no standard way of adding cross-object fields as merge fields in Lightning Email Templates. But there is a workaround that we will discuss in this blog post.

Problem

Unable to add cross-object fields as merge fields in Lightning Email Templates.

For Example:

{{{Account.Contact}}} will be a valid merge field but {{{Account.Contact.FirstName}}} won’t be accessible or considered valid.

Solution

Following is the workaround I implemented in Salesforce to add cross-object fields as merge fields.

Create a Formula Field

The simplest way to get the cross-object level value to the object level is to create a formula field on the sObject that we will use as the primary source in the Lightning Email Template For example, if we want to access a field on the third level of object crossing, we can achieve this with the cross-object formula.

CustomObject1__r.CustomObject2__r.CustomObject3__c.CustomField__c

Use Formula Field as Merge Field

Now once, we have created the formula field to access cross-object fields, we can simply use this field as a merge field in the Lightning Email Template.

For example, if we create a cross-object formula field in the Contact object, we can access it in the Lightning Email Template via the following syntax.

{{{ Contact.CustomCrossObjectFormulaField__c }}}
cross-object fields as merge fields in Lightning Email Templates
Usage of Cross-Object Formula field in Lightning Email Template

And that will be enough to solve your problem. However, I do want to discuss an alternative approach.

Alternative Approach

This approach doesn’t add cross-object fields as merge fields in Lightning Email Templates, however, it is a way of getting dynamic cross-object fields without the usage of merge fields.

For this, I will create an email template and will include my own custom tags, let’s say #CASE_ID, #PROJECT_NUMBER, etc. I always make the tags in all caps and start them with ‘#’ so that it is different from the rest of the email template’s body.

Once the template is done, I get that template body in Apex and individually replace all of the custom tags or custom merge fields with the values I desire them to be. These values are fetched from SOQL easily, without creating various formula fields.

This is just another way when I solve the merge field problem in Salesforce Lightning Email Templates. Whenever a lot of cross-object fields are required, instead of creating separate formula fields, I use this alternate approach that utilizes Email Template, SOQL, and Apex/Flow (for String replacement).

How would you solve the problem of cross-object fields as merge fields in Lightning Email Template? Let’s discuss the comments.

There is a very important article I wrote related to implementing Grid System in Visualforce that can come in handy when designing PDFs, so check it out as well.

For more weekly guides, keep visiting Salesforce Guides.

Also Read

Was this helpful?

Thanks for your feedback!

Talha Saqib

About Author

As a Salesforce Certified Developer, Talha Saqib is an expert in designing and implementing custom Salesforce solutions that help businesses achieve their goals. With 3+ years of experience in the Salesforce ecosystem, Talha has a deep understanding of the platform and is expert in Sales Cloud, Service Cloud, Experience Cloud, REST APIs, Aura and more.

Leave a comment

Your email address will not be published. Required fields are marked *

You may also like

correct format of DateTime field in Web-To-Lead form
Declarative

The correct format of DateTime field in Web-To-Lead form in Salesforce

So, I was recently working with a Web-To-Lead form in Salesforce and realized that all my values were being inserted
Convert Country Code to Country Name
Declarative

Convert Country Code to Country Name in Google Sheets for Data Import Wizard

You would also have faced the same problem time and time again. You are given an Excel or CSV file