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?


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.


Review below Sample snippet:

public static void Testnotification (Test__c test){   
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();   
   message.setSenderDisplayName(‘Test Support’); 
   message.optOutPolicy = 'FILTER';
    message.subject = 'Test Message';
    message.plainTextBody = 'message body.';
   EmailTemplate emailTemplate = [Select Id,Subject,Description,HtmlValue,DeveloperName,Body from EmailTemplate where name = 'lightning template'];  message.setTemplateID(emailTemplate.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.



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.