function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Bablu Kumar PanditBablu Kumar Pandit 

how to use lightning email template via apex

AbhishekAbhishek (Salesforce Developers) 
Hi,

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.

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

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

Thanks.
David Roberts 4David Roberts 4
To use the template:
//whoId the recipient contact
//WhatId = the object expected in the template
message = Messaging.renderStoredEmailTemplate(emailTemplate.id, '0038E00000ToJ43QAF', 'a2M8E000001T5r0UAC');
followed by other settings:
message.setUseSignature(false);
message.setSenderDisplayName(senderEmailAddress); 
message.setReplyTo(replyEmailAddress);
message.optOutPolicy = 'FILTER';
message.setUseSignature(false); 
message.setBccSender(false); 
message.setSaveAsActivity(false);   

message.toAddresses = new String[] {contactEmailAddress};  
Messaging.SingleEmailMessage[] messages = new List<Messaging.SingleEmailMessage> {message};  
Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);

//results display...