0

I have seen a ton of posts regarding this issue where we cant set an user as targetObjectId and an object as WhatId when we are using an Email Template. Then I came across some posts using Messaging.renderStoredEmailTemplate where we have a workaround to use running user as whoId and a Standard or Custom Object as WhatId.

I have to use a Lightning Email Template for this scenario based on certain requirements.

Here is my code, it doesn't produce any errors, but emails are not sent.

@AuraEnabled
public static void sendThankYouEmail2(Id caseId) {
    //String[] recipients = new String[] { UserInfo.getUserEmail() };

    EmailTemplate emailTemplate = [SELECT Id FROM EmailTemplate WHERE DeveloperName = 'Test_thank_you'];
    Messaging.SingleEmailMessage thankYouMail = Messaging.renderStoredEmailTemplate(emailTemplate.Id, UserInfo.getUserId(), caseId);
    thankYouMail.setSaveAsActivity(false);
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { thankYouMail });

}

Update after adding recipients -

@AuraEnabled
public static void sendThankYouEmail2(Id caseId) {
    String[] recipients = new String[] { UserInfo.getUserEmail() };

    EmailTemplate emailTemplate = [SELECT Id FROM EmailTemplate WHERE DeveloperName = 'Test_thank_you'];
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    mail.setToAddresses(recipients);
    Messaging.SingleEmailMessage rMail = Messaging.renderStoredEmailTemplate(emailTemplate.Id, null, caseId);
    mail.setSaveAsActivity(false);
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

}
6
  • 2
    I don't think renderStoredEmailTemplate() sets the recipient. I think you should add the recipients addresses using thankYouMail.setToAddresses(listOfAddresses); Nov 4, 2021 at 18:51
  • @rael_kid I have updated my post with the modifications. I tried it doesn't produce any errors, but does not work. Nov 4, 2021 at 19:42
  • 2
    Is email deliverability set to “all emails”? Nov 4, 2021 at 19:47
  • Yes it is set to All Email Nov 4, 2021 at 19:49
  • The code currently in the question is kind of messed up, first you're instantiating a new SingleEmailMessage, but then you're assigning the result of renderStoredEmailTemplate to a new variable... Could be a copy-paste mishap though. Nov 4, 2021 at 19:54

1 Answer 1

0

Thanks @rael_kid for the guidance -

There were two issues I had with my code, one was not supplying the toAddresses and the second one was not adding the renderStoredTemplate to the SingleMailMessage methods. The below code fixes all that and sent out the email -

@AuraEnabled
public static void sendThankYouEmail2(Id caseId) {
    String[] recipients = new String[] { UserInfo.getUserEmail() };

    EmailTemplate emailTemplate = [SELECT Id FROM EmailTemplate WHERE DeveloperName = 'Test_thank_you'];
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    mail.setToAddresses(recipients);
    Messaging.SingleEmailMessage rMail = Messaging.renderStoredEmailTemplate(emailTemplate.Id, null, caseId);
    mail.setSaveAsActivity(false);
    mail.setSubject(rMail.getSubject());                                     
    mail.setPlainTextBody(rMail.getPlainTextBody());
    mail.setHTMLBody(rMail.getHtmlBody());
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

}
2
  • for the community - it would be useful to state why this solution solved the original issue
    – cropredy
    Nov 5, 2021 at 1:07
  • 1
    @cropredy I have put a short explanation of what was wrong with my code. Nov 5, 2021 at 19:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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