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
Iswarya SekarIswarya Sekar 

Hi everyone!! I don't know how to use Email templates to send mail in Scheduler apex. Anyone please help me to solve this!!

Best Answer chosen by Iswarya Sekar
Raj VakatiRaj Vakati
Sample code is here. Modify code based on your requirement.
 
global class scheduledMerge implements Schedulable {
    global void execute(SchedulableContext SC) {
        
        sendEmail();
        
    }
    global  void sendEmail() {
        
        Contact con = new Contact();
        con.FirstName = 'Test';
        con.LastName = 'Contact';
        con.Email = 'no-reply@organization.com';
        insert con;
        // Create Email and Send
        Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();
        msg.setTemplateId('Your Email Template Id');
        msg.setCcAddresses(new String[] {'email1@recipient.com', 'email2@recipient.com'});
        msg.setTargetObjectId(con.Id);
        msg.setWhatId('Your Record Id if applicable');
        msg.setSaveAsActivity(false);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { msg });
        // Don't Forget!  Clean up!
        delete con;
        
    }  
    
}

 

All Answers

Raj VakatiRaj Vakati
Sample code is here. Modify code based on your requirement.
 
global class scheduledMerge implements Schedulable {
    global void execute(SchedulableContext SC) {
        
        sendEmail();
        
    }
    global  void sendEmail() {
        
        Contact con = new Contact();
        con.FirstName = 'Test';
        con.LastName = 'Contact';
        con.Email = 'no-reply@organization.com';
        insert con;
        // Create Email and Send
        Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();
        msg.setTemplateId('Your Email Template Id');
        msg.setCcAddresses(new String[] {'email1@recipient.com', 'email2@recipient.com'});
        msg.setTargetObjectId(con.Id);
        msg.setWhatId('Your Record Id if applicable');
        msg.setSaveAsActivity(false);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { msg });
        // Don't Forget!  Clean up!
        delete con;
        
    }  
    
}

 
This was selected as the best answer
Iswarya SekarIswarya Sekar
Hii raj, When Case status is closed and CLOSED__c is True, it should send an email alert. for this, what should i do?