+ Start a Discussion
Bablu Kumar PanditBablu Kumar Pandit 

how to use lightning email template via apex

AbhishekAbhishek (Salesforce Developers) 

Lightning email templates are stored under 'Email Template' and this should work with SingleEmailMessage as well.
Also haven't seen any limitations to use the lightning template in apex in the 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.


I hope you find the above information is helpful. If it does, please mark as Best Answer to help others too.