Is there any good resource where its explained clearly about the below functions of Single Email Messaging class and their usage:

  1. renderStoredEmailTemplate
  2. setWhatId
  3. setTargetObjectId
  4. setTemplateId

Additionally,an example program for sending mail from salesforce where 'email templates' are involved will be helpful as well. Have gone through the salesforce documentations but they are not clear enough.

  • 1
    Hi and welcome to SFSE. Please take a minute to read How to Ask and perhaps take the tour so you understand the requirements of this community, then edit your question to add missing detail. This site is here for specific questions about specific technical problems and the question must demonstrate an attempt to research or resolve the issue. As it stands, this question is likely to be closed without answer.
    – Phil W
    Aug 18 at 6:52

renderStoredEmailTemplate renders a template. In other words, it produces output that would be the email that would be sent to the recipient with all the merge fields merged. This is typically used to provide a "mail preview" without actually sending the message. This replaces the previous "trick" where developers would send the email, then roll it back, just to get a rendered preview.

setWhatId sets the "related to" for the email template. This can be an Account, Opportunity, Case, Custom Object, etc. This allows the email to be related to a type of record, or in other words, what topic the email is regarding. This is optional when emailing Leads and Contacts, and cannot be set when emailing Users.

setTargetObjectId sets the "recipient" for the email template. This is a Lead, Contact, or User. It's the person you're trying to email. The Email field from that record will be used to address the target.

setTemplateId sets the template you'd like to use for the email message. Once sent, merge fields from the "related to" and "recipient" will be merged in place to provide personalized emails.

Sample Code Listing

// Before running this code, create an email template
EmailTemplate template = [SELECT Id FROM EmailTemplate WHERE Name = 'CaseUpdated'];
// Just an example record
Case caseRecord = [SELECT ContactId FROM Case WHERE ContactId <> NULL LIMIT 1];

Messaging.SingleEmailMessage[] messages = new Messaging.SingleEmailMessage[0];
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();

// Note: If you need to send multiple emails, add them all to a list and send at the end

// The template to use

// Who we're mailing; the contact related to the case

// The related record we're talking about

// We need to send a list, even if just one message

Realistically, sending emails are incredibly simple. If the documentation doesn't help, try writing a few programs of your own and experiment. There's no code you can write that can cause any permanent damage, so try and experiment.

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