enter image description here

another newbie VS question.

This is how far i got (pls cf picture)

  • get a list from available templates from a certain directory
  • add each selected template into editable textarea
  • use the edited textarea as Email Body
  • choose Contact by Lookup field, add subject, cc, bcc
  • send the Email

What is obviously missing is the merging of the fields in the Email body.

I had a look on the SingleEmailMessage(); Documentation (http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_sendemail.htm#SingleEmailMessage and also on this post https://developer.salesforce.com/forums/ForumsMain?id=906F000000094ClIAI)

It seems to me that things are getting very, very tricky at this point, if its even possible at all: As I understood in order to merge the fields approriately, I need the templateId, whatId and TargetObjectId.

I got no problem with TargetObjectId. For WhatId it seems to be the case that might differ for each selected EmailTemplate, but if the merging of fields is really depenent on templateId, too - I can not think of any way to elegantly achieve the parsing of the templates - as I cannot provide the sendEmail method with different templateIds.

As my employer wants to be able to combine and edit templates on the fly, I was wondering if there is any way to achieve correct field merging without doing RegEx and stuff myself...

If there is no other way, please point me in the right direction - I am at loss. Thank you for your input, Sz

PS: Is there maybe a way to merge/parse the template fields BEFORE they get inserted into my textarea? i.e. get the merging done, and insert those already evaluated fields. I have not found any viable way to achieve this by searching this forum and others.

1 Answer 1


If I understand you correctly, you want to be able to execute SFDC merge functionality and display the results for the user. If so, see https://developer.salesforce.com/forums/ForumsMain?id=906F000000095j1IAA for an idea - basically, you write your own merge processor by querying all fields of the target object and then doing apex string replace operations into the email template.

For example, if body is the apex variable representing the email template body, and the email template is about Contacts, after querying all fields on Contact for the contact id in question, do body.replaceAll('{!Contact.email}',contact.email);

This approach has some limitations:

  1. It assumes that all merge expressions are simply field expressions and not formulas
  2. You'll need to be aware of parent or lookup relationships as those fields can also be referenced in email templates
  3. Then there are the $User, $profile, etc global variables
  4. There are case sensitivity issues as the merge field could be {!contact.EMAIL} or other such variation
  • Thank you. Yes, thats basically what we are intending to do. We have settled for fewer requirements for a start, but I will keep your advice in mind. Thanks for your input. Commented Mar 25, 2014 at 17:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .