0

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

1
  • 1
    can you please explain a bit more on what you are trying to do, I am at least having a very hard time following what you are asking for.
    – Rao
    Commented Oct 9, 2014 at 15:39

2 Answers 2

3

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
}
3
  • you might want to annotate the code a bit to indicate why the secondPreviewEmail is invalid. Good hack
    – cropredy
    Commented May 12, 2015 at 1:24
  • 1
    Summer 16 resolves this hack here: salesforce.stackexchange.com/a/120098/2602
    – cropredy
    Commented May 1, 2016 at 23:20
  • This post made me laugh ... very smart hack :) Commented Jul 25, 2018 at 1:27
1

Ok, I think this question has been asked many times ...how to get the rendered email template and save/use it somewhere.

And the answer is found here through a rather clever hack -- https://salesforce.stackexchange.com/a/8745/2602

Involves constructing the email and then doing a rollback to get the rendered value.

5
  • Hi @crop1645, i followed the above link, i am using the code in for loop as i mentioned earlier sending more email, i am getting error like "you have uncommitted work pending. please commit or rollback before calling out salesforce" , where i am doing wrong
    – Sunny
    Commented Oct 10, 2014 at 9:56
  • Sunny, without knowing more details -- and I think you should post a new question to deal with this issue, you should look at items like this: help.salesforce.com/apex/… as well as Google you have uncommitted work pending. please commit or rollback before calling out as this error relates to callouts and dml
    – cropredy
    Commented Oct 10, 2014 at 18:18
  • Hi @crop1645, I am using batch apex to send email to contacts using amazon ses, there i am using the above process to get email template body and send to contact. There i am making for loop to send multiple emails, and i even tried with making batch size as 1, still i am getting error as "you have uncommitted work pending. please commit or rollback before calling out"
    – Sunny
    Commented Oct 12, 2014 at 7:23
  • Sunny - please post as a new question on SFSE your issue with uncommitted work pending - you'll need to post your code as well (stripped down to the essentials to avoid code dumping on us)
    – cropredy
    Commented Oct 13, 2014 at 14:54
  • Hi @corp1645, i posted my issue with code, can you please look into it and suggest me where i am doing wrong,salesforce.stackexchange.com/questions/53191/…
    – Sunny
    Commented Oct 14, 2014 at 8:58

You must log in to answer this question.

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