Skip to main content
added 66 characters in body
Source Link
cropredy
  • 73.3k
  • 8
  • 128
  • 283

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 }

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
}

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 }

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
}
Source Link
Kevin Lam
  • 699
  • 4
  • 9

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 }