I am developing a feature to send a email from a Custom Visualforce page. For this I am using the Messaging.SingleEmailMessage class.

I need to give users, the option to select email templates from a set of email templates in the Salesforce 'Email template'.

However i cannot find a way to preview emails which will be sent. My email templates will have data mapped from the respective record. How can i solve this problem of previewing the email templates with the values from email templates?

Please suggest.


In short, there is no simple 'preview' function available to you. You could work around it using Visualforce pages/components for the preview. Assuming you are using Visualforce Email Templates, you could do something like this:

  1. Copy the relevant content out of the Visualforce Email Template into a Visualforce Component
  2. In your Email Template, reference the component e.g. <c:EmailContent obj="{!relatedTo}"/> to render the content
  3. Create a new Visualforce page, also referencing the component (as #2)
  4. Use the Visualforce page just created as a preview

If you wanted to show your user a list of Email templates to choose from, you could simply query the EmailTemplate object with some valid criteria, e.g.

Select Name from EmailTemplate Where IsActive = true
  • Thanks for the reply. I understand the solution you suggested can be applied if there is a single template. But i will have a choice a email template which can be selected while sending. How can i handle that?
    – sagar
    Jan 27 '15 at 11:27
  • Modified answer... Jan 27 '15 at 12:19
  • @sagar I think your answer may still be here. If you have multiple templates you want them to consider, the real question is how do I allow them to send the one they want. If each template has it's own component for content, simply showing the component options (apex:tabPanel possibly or dropdown that causes a re-render) can be a relatively simple matter. From there you just need to figure out how the user will select the one they want and build an action to send only the one they selected.
    – Tezyn
    Jan 27 '15 at 12:25
  • 1
    there is an alternative solution here: salesforce.stackexchange.com/a/8745/2602
    – cropredy
    Jan 27 '15 at 20:30
  • wow, a super hack - very resourceful Jan 27 '15 at 22:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.