[SalesForce] How to get Email Template executed body

I am working on sending email by using 3rd party application like Amazon SES

When i query for perticular email template i am getting body whaever the email templated had

like
Email Template body
Hello {!case.name}

I am retriving only above body not like executed body
Hello CaseName(records name).

As i got sence like without using case id how can it get name.

But i am wondering that how i need to send case id dynamically and how i can get executed body

Thanks in advance

Best Answer

I did something similar for previewing an email before it's being sent. The idea is to set up two emails in a list, one correctly and the other one not, try and send them and retrieve the generated email body in the catch statement:

Messaging.SingleEmailMessage[] previewEmails = new Messaging.SingleEmailMessage[]{};
Messaging.SingleEmailMessage firstPreviewEmail = new Messaging.SingleEmailMessage();
firstPreviewEmail.setUseSignature(false);
firstPreviewEmail.setSaveAsActivity(false);
firstPreviewEmail.setTemplateId(templateId);
firstPreviewEmail.setTargetObjectId(UserInfo.getUserId());
firstPreviewEmail.setWhatId(dealId);
previewEmails.add(firstPreviewEmail);
Messaging.SingleEmailMessage secondPreviewEmail = new Messaging.SingleEmailMessage();
previewEmails.add(secondPreviewEmail);
try {
    List<Messaging.SendEmailResult> previewResult = Messaging.sendEmail(previewEmails);
}
catch (Exception e) {
    generatedEmailBody = firstPreviewEmail.getHtmlBody();  // Email body generated
}
Related Topic