Saturday, September 29, 2012

Custom setting values in Salesforce email templates

Yesterday, as I was playing with a simple CRM idea in Salesforce, I came across an unexpected roadblock:  There didn't seem to be a way to merge custom setting values into an email template. A search on Google seemed to confirm this suspicion, as another admin asked the same question and arrived at the conclusion, "No, it's not possible." But, trusting Salesforce's immense power and flexibility, and hoping at all costs to avoid Apex code, I pursued my gut feeling that there must be a way.

And without disappointment, I discovered through some quick experiments that Salesforce does indeed make it possible to merge custom setting values into email templates.

The idea is simple: Put the custom setting value into a formula field on the desired object, and then merge the formula field value into the email template.

Below is an example of how it could work for you. Let's say that you want there to be a default level of optimism associated with every contact in your org. Imagine that you intend to merge this optimism into your automated emails. How would you do it?


  1. Create a new custom setting labeled "Optimism Setting" with Setting Type set to "Hierarchy". The object name (a.k.a. API name) should be set to Optimism_Setting.
  2. Within Optimism Setting, create a new text field labeled "Default Forecast". The field name (a.k.a. API name) should default to Default_Forecast.
  3. Looking at the Optimism Setting page, click Manage and then set the default organizational level value for Default Forecast to "blue skies and sunshine".
  4. On the Contact object, create a new formula field labeled "Optimism Forecast". The field name should default to Optimism_Forecast.
  5. Insert the custom setting value for Default Forecast into this field. Your formula should look like this: $Setup.Optimism_Setting__c.Default_Forecast__c
  6. Save the field, and now you're ready for email merge!

At this point, you should be able to inject the custom setting value for Default Forecast into any email template simply by including the {!Contact.Optimism_Forecast__c} merge field.

Note:  If you do complete this exercise, I highly recommend keeping the Default Forecast at "blue skies and sunshine" in your org.