Supercharging your Salesforce Email Templates

For all of the awesome features and power of Salesforce, it doesn’t take an admin very long to run into something bafflingly underpowered. Like, for instance, email templates. Automated emails are easy to trigger based on sophisticated criteria, but giving those emails the personalized touches expected in this day and age? Not so straightforward.

Associations and nonprofit organizations can make a lot of use of automated emails: thank you emails for online donations, membership renewal notices, event registration confirmations, welcome emails for new members, and acknowledgements to renewing members. And it gets more complicated from there, if you have different classes of membership, offer certifications, or accept donations with your renewals or event registrations.

Feeling Cross?

The first wall your head is going to encounter when setting up templates for automation is this: you can only merge in fields from the object that triggers the email. It stands to reason that if you’re triggering a donation thank-you to a contact from a donation object linked to that contact, that you could just merge in the Contact.FirstName. But no.

The solution to this problem is to create cross-object formula fields on the triggering object, then merge those fields into your email template. Here’s a good tutorial on setting up templates and creating basic cross-object formula fields. There are also some good tips on other aspects of email templates here.

Once you know your way around formula fields, this becomes a great jumping off point for additional personalization.

Formula Friendly

You can insert formulas directly into your email template. A common and useful example is to use the function BLANKVALUE to replace any missing primary data, for instance

Dear {!BLANKVALUE(Contact.FirstName, "Friend")}

will fill in “Friend” for any contact where the first name is missing. As noted here, there is even shorthand for this – you can paste in your merge field, then type a comma and the desired replacement text, like so

 {!Contact.FirstName, Friend}

and, upon save, Salesforce will convert this to the blankvalue syntax, as above. There are some other examples of using formulas directly in email templates here.

That said, in most circumstances, I prefer to use formula fields on the triggering object. There are a lot of good reasons why:

  • It is easier to check syntax and see the results across a variety of records. (Eventually, you’ll probably want to hide these formula fields, but it’s very handy to have them visible on an object layout while you’re working out the kinks.)
  • Your hard work is protected as a formula field, and won’t get trashed by a careless email edit.
  • You can more easily reuse the fields across different templates.
  • And, of course, you have access to linked objects, which is crucial for advanced personalization.

My most involved email schemes were all developed for the League of American Bicyclists, who use Fonteva’s MemberNation Salesforce app. The following examples all reference MemberNation 4 fields and objects, but the basic concepts can be adapted for your own architecture and needs.

Using an example I referenced above, this formula field is used to acknowledge donations that are added onto membership dues payments.

if (not(isblank( MN4__Donation__c )),BR() & 'Thanks, also, for donating an additional $' & Text(MN4__Donation__r.MN4__Total_Amount__c) & ' to your membership gift. Your donation shifts our time-tested programs into high gear!','')

There’s a lot going on here, so I’ll break it down.

if (not(isblank( MN4__Donation__c )),

It’s all wrapped in the IF function, which takes three arguments — a logical condition, the value to return if that condition is true, and then the value to return if the condition is false. In a simple, non-code paraphrase, if(meeting with a donor, wear suit, wear shorts and t-shirt). For this formula, the condition is looking at the lookup field that connects the membership renewal with a donation, if a donation is present.

BR() & 
'Thanks, also, for donating an additional $' & 
Text(MN4__Donation__r.MN4__Total_Amount__c) & 
' to your membership gift. Your donation shifts 
our time-tested programs into high gear!'

If the MN4__Donation__c field is not blank, there is a donation attached, and we want to generate a sentence to insert in the email to recognize that donation. In this case, it’s actually a short paragraph. You can’t use HTML in these merge formulas, so my true condition starts with BR(), which will insert a paragraph break before the text. Then I add the desired text, merging in an amount from the related Donation object, wrapped in the TEXT function because Salesforce will complain otherwise.


If there’s no donation attached, I want this field to be blank, so the false condition is an empty string.

Here’s how I use this field in the email template – the field is jammed onto the end of the first paragraph. Thanks to the added line break, it splits off into its own paragraph if a donation is present, but leaves no awkward gap if there isn’t an added donation.

We’re thrilled to have you with us. Your support empowers the League to strengthen communities and advance bicycling for all Americans, be it through our Bicycle Friendly America program, federal advocacy, or our Smart Cycling initiative. Thank You! {!MN4__Order__c.Member_donation_thank_you_text__c}

Always remember to preview your email template with the different variations merged in. It’s very easy to make sloppy-looking spacing mistakes.

Subject to Change

It’s not obvious from the interface, but you can use merge fields in the email subject. This allows you to do some straightforward personalization for subject lines like Sparky, did you realize that your membership expired last night? Surely not! but you can also use formulas like this one, which looks at whether the membership is new or a renewal, and generates the appropriate subject line:

If( MN4__Membership__r.MN4__Renewal__c ,'Thank you for renewing your League membership!','Thank you for joining the League in membership!')

Hitting the Links

Another non-obvious feature is that you can use merge fields in link urls. The League’s membership page takes a member directly to their personal renewal page when there’s a Salesforce ID as a url parameter. You can merge in just the ID from the object, like so:{!}

but I use this link in several places, so I compose the whole link in a formula field, and paste that merge field as the entire contents of the link. Here’s my formula:

''+ Id

The Power to Simplify

Using formulas and formula fields creates a more personal and specific email for your member or donor. However, it can also make your life as an admin much simpler. A brute force approach to the problems above might easily result in four very similar email templates:

  1. New membership welcome email
  2. New membership welcome email with donation thank you
  3. Renewal acknowledgement email
  4. Renewal acknowledgement email with donation thank you

If you have different classes of membership, you might even have two or three times as many. To go with those templates, you’d have a complicated process builder flow to sort the memberships to the appropriate email.

Or, to avoid the overhead of keeping up, you might have an extremely generic email you toss at all memberships.

Thanks for joining, or welcome back, pal! If you added a donation, well, thanks even more!

With formula fields, and some clever planning of the email content, you could handle all four (or eight, or twelve) of those scenarios with a single email that automatically contains the appropriate text.

I hope these tips and tricks for Salesforce email template merges are helpful – please share your own in the comments.

Posted in Salesforce Admin How-tos.

Leave a Reply

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