1

I'm trying to send a 30 days before renewal email reminder, based on a date field on the Account, and want to run a daily Scheduled Job to grab these relevant accounts and send out the email template.

I know we can use Classic Email Templates and send them via Messaging.SingleEmailMessage, but the official docs don't mention support for the new Lightning Email Templates.

Can you send Lightning Email Templates in Apex Code? Or is it better to just stick with Classic Email Templates?

0

Do you see any error message when you use 'Messaging.SingleEmailMessage'?

Lightning email templates are stored under 'Email Template' and this should work with SingleEmailMessage as well.

Also haven't seen any limitations to use lightning template in apex in below documentation link.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_single.htm

Review below Sample snippet:

public static void Testnotification (Test__c test){   
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();   
message.setTargetObjectId(contact.id); 
   message.setSenderDisplayName(‘Test Support’); 
   message.setReplyTo(‘no-reply@test.com’);
   message.optOutPolicy = 'FILTER';
    message.subject = 'Test Message';
    message.plainTextBody = 'message body.';
   message.setUseSignature(false); 
   message.setBccSender(false); 
   message.setSaveAsActivity(false);   
   EmailTemplate emailTemplate = [Select Id,Subject,Description,HtmlValue,DeveloperName,Body from EmailTemplate where name = 'lightning template'];  message.setTemplateID(emailTemplate.Id); 
  message.setWhatId(account.Id); 
  message.toAddresses = new String[] { contact.email};  
  Messaging.SingleEmailMessage[] messages = new List<Messaging.SingleEmailMessage> {message};  
  Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
  if (results[0].success) {
    System.debug('The email was sent successfully.');
} else {
    System.debug('The email failed to send: '
          + results[0].errors[0].message);
}

Advantages of Lightning Templates.

If you’re using Lightning Email Templates, you have the full range of public, private, and specified sharing capabilities for who can use what and it helps enforce communication standards by making the development of templates and communication.

https://help.salesforce.com/articleView?id=email_templates_lightning_parent.htm&type=5

Thanks

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.