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
Akshay ReddyAkshay Reddy 

Sendemailmessage using email template as source for the body

public class Task_triggerFunctions
	{
    public static void sendEmailAlert(List<Task> Tasks) 
        {
        List<Delivery_list_email__mdt> dls = [select label, email_id__c from Delivery_list_email__mdt];
        String[] options;
        List<String> sendTo = new List<String>();
            for (Task TaskRecord : Tasks) 
            {
                if (TaskRecord.Send_email_alert__c != null && TaskRecord.Delivery_lists__c != null) 
                {
                    EmailTemplate emailTemplate = [select Id, Subject, HtmlValue, Body from EmailTemplate where DeveloperName =: 'Task_alert'];
                    
                    
                    String subject = emailTemplate.Subject;
                    subject = subject.replace('{!Task.Subject}', TaskRecord.Subject);
        
                    String htmlBody = emailTemplate.HtmlValue;
                    htmlBody = htmlBody.replace('{!Task.Subject}', TaskRecord.Subject);
                    htmlBody = htmlBody.replace('{!Task.TaskSubtype}', TaskRecord.TaskSubtype);
                
                    String plainBody = emailTemplate.Body;
                    plainBody = plainBody.replace('{!Task.Subject}', TaskRecord.Subject);
                    plainBody = plainBody.replace('{!Task.TaskSubtype}', TaskRecord.TaskSubtype);
        
                  
                    
                    
                    //Create an instance of SingleEmailMessage class
                    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                    List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
                    //Subject of the email
                    mail.setSubject(System.Label.task_email_alert_subject + TaskRecord.Subject);
                                        
                    mail.setHtmlBody(htmlBody);
                    mail.setPlainTextBody(plainBody);
                    // Get the multi-picklist values to an array of Strings using split.            
                    options = TaskRecord.Delivery_lists__c.split(';');
                    if(options.size()>0)
                    {	
                        //Get the Delivery list Email address from Custom Metadata and store them in the list(sendTo) 
                        for(String option : options)
                            {
                                for(Delivery_list_email__mdt dlss : dls)
                                {
                                    if( option == dlss.Label)
                                    sendTo.add(dlss.email_id__c);	
                                }
                            }
                    }
                            
                    system.debug('mail ids' + sendTo);
                    //Add the 'to Addresses' we stored in the list (sendTo)
                    mail.setToAddresses(sendTo);   
                    //Add the email to the master list
                    mails.add(mail);
                    //Send all emails in the master list
                    Messaging.sendEmail(mails);                      
                }   
            }
		}
	}

I've written a class wherin the system will composed the email body using plaintext.replace. 

The main intention I've used the email template is that the admins should be able to edit the email template at any time and system should get the field values automatically. 

But in my logic I've to update both the email template and class as well.

but I want it to be more dynamic that whenever a new field is added in the email template, the class should get that new field automatically.

Could anyone help me here.