I need to write an apex method that send an email which is related to a custom object. The email will use a template which used merge fields from the custom object. How can I specify the custom object record when I'm coding this? I only found how to specify related Contacts, Users, Products, etc...


There are couple of ways to accomplish this task. Please check my blog post titled 'Dynamically populating custom HTML email template content in Force.com with custom dynamic data using Apex' which uses custom HTML email templates to send email with data populated from both standard object and dynamically computed data. The code referenced in this article uses SingleEmailMessage to send emails. It can take both email addresses of the recipients or the Salesforce Ids (if it's Salesforce user, contact, etc...) which doesn't count against the daily limits.

  • You beat me to the answer. Great Blog post. Very useful. Thank you Sep 5 '13 at 16:28
  • I've implemented your solution and its really cool. I'm using my HTML with letterhead template and I have two questions: (1) I'm not seeing my letterhead logo, why? (2) I see ']]>' at the end of my email, I guess it comes from the HTML markup, how can I get rid of it?
    – Dedo
    Sep 10 '13 at 16:15
  • 1. I believe you have uploaded your logo to 'Documents'. There is a property labelled 'Externally available image'. Did you check that? 2. I had the same issue, that's why I went with Custom (without using letterhead) template. I did try replacing the ']]' character(s), but then the email didn't display anything, which is obvious, because ']]' needs to be there to properly close the CDATA section. Sep 12 '13 at 19:51

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.