My Scheduler batchable class
global class SendContactEmail implements Database.batchable<sObject> , Database.Stateful{
private EmailTemplate emailTemp;
global SendContactEmail(){
emailTemp = [Select Id From EmailTemplate Where Name = 'Template'];
}
global Database.QueryLocator start(Database.BatchableContext BC){
String query = 'Select Id, Lastname, Firstname, Email From Contact WHERE (Id IN (select officer__c from Lead WHERE RecordTypeId = \'01233000000ABCD\' ) AND RecordTypeId = \'01239000000EFGH\' )';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Contact> scope){
try{
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
for(Contact con : scope) {
if(emailTemp != null && con.Email != null && con.Email != '') {
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setToAddresses(new List<String>{con.Email});
email.setTargetObjectId(con.Id);
email.setTemplateId(emailTemp.Id);
email.setTreatTargetObjectAsRecipient(true);
emails.add(email);
}
}
Messaging.sendEmail(emails);
}
catch(exception ex){
}
}
global void finish(Database.BatchableContext BC){
}
}
Test Class:
@istest(SeeAllData=true)
class SendContactEmailBachable_Test{
static testmethod void test() {
System.Test.startTest();
Contact con = new Contact();
con.Lastname = 'testing';
insert con;
Id batchJobId = Database.executeBatch(new SendContactEmail(), 200);
System.Test.stopTest();
}
}
Scheduler Class:
public class SendContactEmailScheduler implements Schedulable {
public void execute(SchedulableContext context) {
Id batchJobId = Database.executeBatch(new SendContactEmail(), 200);
}
}
Scheduler Test Class:
class SendContactEmailScheduler_Test{
static testmethod void test() {
System.Test.startTest();
Contact con = new Contact();
con.Lastname = 'testing';
insert con;
SendContactEmailScheduler sh1 = new SendContactEmailScheduler ();
String sch = '0 0 23 * * ?';
system.schedule('SendContactEmailScheduler_test', sch, sh1);
SendContactEmail sc = new SendContactEmail();
Database.executeBatch(sc, 200);
sc.execute(null,[select id,Email from contact]);
System.Test.stopTest();
}
}
Everything runs perfectly. No errors, batches processed without any errors. But emails are not being sent. Checked the Debug log, no errors of emails not being sent. Could anyone suggest what could be the reason emails are not being sent. It was working fine before. Recently enabled Salesforce Engage in Prod, don't know if that is the problem.
Thanks.