1

I have an email template which I am calling it from a work flow rule. Also I have a check box in contact object, when I click on check box the email template will send email to the contact email address. Its working fine.

But I want to know how we can implement controller side. I mean a have a page where I am entering contact details(with email id), when I click on Save i am able to save the record and those records I am displaying in a different table of same page. So from the second table, I will select one contact and when I will click on button(something called send email) in that page, it should send an email to selected to contact.

I am just looking for the logic on how to call an email template from controller and send email to selected record.

1 Answer 1

1

This is a very simple piece of code

EmailTemplate templateId = [Select id from EmailTemplate where name = 'Your template Name'];
List<Messaging.SingleEmailMessage> allmsg = new List<Messaging.SingleEmailMessage>();
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTemplateID(templateId.Id); 
mail.setTargetObjectId(contactId)
mail.setSaveAsActivity(false);
allmsg.add(mail);
Messaging.sendEmail(allmsg,false);

Use this piece of code to send email from apex using template.

Update: Or you can create a utility class as suggested by @cropredy. You can find reference here.

3
  • 1
    FWIW, there is a nice fluent pattern to exploit SFDC outbound email services that I've added into my orgs
    – cropredy
    Commented Jun 15, 2016 at 18:59
  • @Tushar, I checked the utilityclass idea, but there we are not selecting any email template and instead of that we are using messaging methods in that class. i checked the messaging methods and it doesnt have any method to select the Email template developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
    – KSL
    Commented Jun 16, 2016 at 8:36
  • Ok Thanks Tushar... I can use the above format in my code level
    – KSL
    Commented Jun 16, 2016 at 19:37

You must log in to answer this question.

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