0

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.

2
  • I assume you've verified the Email Deliverability setting already? Commented Feb 9, 2018 at 20:31
  • Yes I did check that as well.
    – Lin
    Commented Feb 9, 2018 at 20:49

1 Answer 1

3

This (present in the execute method) is 99% of the time a bad thing to do:

 try {
     // Some code goes here
 } catch(Exception ex) {    
 }

because it is catching and throwing away information about any problem that may have occurred. The code will appear to have worked because the platform's batchable execution code does not receive the exception and so thinks everything is OK. This is referred to as "silent failure" and makes problems much harder to track down.

I would start the diagnosis process by removing that try/catch and see what turns up in the Apex Jobs log. I don't know what change could have caused the problem; the platform does have daily message limits that can get used up.

4
  • Got it! but I was able to send emails in sandbox using the same code, it's just not sending in production. Also it is not hitting the daily message limit in Prod.
    – Lin
    Commented Feb 9, 2018 at 19:05
  • Is their an email template? If yes, check the access settings. Commented Feb 9, 2018 at 19:26
  • Yes there is a Visualforce Email template. Access Setting?
    – Lin
    Commented Feb 9, 2018 at 20:08
  • Is there any particular setting I need to Look into. I cannot remove try catch in Production because it would send emails.
    – Lin
    Commented Feb 9, 2018 at 21:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .