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
Ryan MeyerRyan Meyer 

Scheduled batch doesn't appear to be executing

Hello, 

I have the following apex class scheduled to run daily. It says it executes, but there is nothing in the logs at that time even using the finest settings, and the intended result (bounced leads stop getting emails) doesn't happen. I'm sure it's something obvious but I'm bashing my head against the wall. Not only is there a lead with a bounced email, but I specifically created a Test Test lead with no success. Any help would be greatly appreciated.

SCHEDULED CLASS:

public class ScheduleStopEmailingBouncedLeadsBatch implements Schedulable {
    public void execute(SchedulableContext ctx) {
        StopEmailingBouncedLeadsBatch SEBL = new StopEmailingBouncedLeadsBatch();
    Database.executeBatch(SEBL,200);
    }
}

CALLED CLASS:

 

public class StopEmailingBouncedLeadsBatch implements Database.Batchable<sObject>{
    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator('select id from lead where (emailbounceddate=yesterday and hasoptedoutofemail=false) or name=\'Test Test\''); 
    }
    public void execute(Database.BatchableContext bc, List<Lead> Leads){
        try{
            for(Lead L : Leads)
                L.HasOptedOutOfEmail=true;
        }
        Catch(Exception e){
            system.debug('Exception occured -->'+E.getMessage()+' at line '+e.getLineNumber());
        }
    }    
    public void finish(Database.BatchableContext bc){
        
    }    
}

Abdul KhatriAbdul Khatri
Have you scheduled the job?

Can you show the screen shot of the scheduled job by Setup--> Quick Search (type job) --> Scheduled Job?
Raj VakatiRaj Vakati
Can you execute the batch class and see whether you are able to fetch the data .. I guess you query is not returning the data .. 
Raj VakatiRaj Vakati
try this code 
 
public class StopEmailingBouncedLeadsBatch implements Database.Batchable<sObject>{
    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator([select id from lead where (emailbounceddate=yesterday and hasoptedoutofemail=false) or name='Test Test']); 
    }
    public void execute(Database.BatchableContext bc, List<Lead> Leads){
        try{
            for(Lead L : Leads)
                L.HasOptedOutOfEmail=true;
        }
        Catch(Exception e){
            system.debug('Exception occured -->'+E.getMessage()+' at line '+e.getLineNumber());
        }
    }    
    public void finish(Database.BatchableContext bc){
        
    }    
}

 
Ryan MeyerRyan Meyer
I should have reworded this. It doesn't appear to be a scheduler issue - it's scheduled and when I execute the code anonymously it does not work either. Running the SOQL query directly itself however does return the leads we want modified.
Ryan MeyerRyan Meyer

Sadly that code didn't work either. I was thinking it was the escapes too. Also tried update Leads; but nothing is getting modified even though select id from lead where (emailbounceddate=yesterday and hasoptedoutofemail=false) or name='Test Test' returns IDs

Abdul KhatriAbdul Khatri
Aren't you missing the update statement after the loop in the execute method.
 
public class StopEmailingBouncedLeadsBatch implements Database.Batchable<sObject>{
    
    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator('select id from lead where (emailbounceddate=yesterday and hasoptedoutofemail=false) or name=\'Test Test\''); 
    }
    
    public void execute(Database.BatchableContext bc, List<Lead> Leads){
        
        try
        {
            for(Lead L : Leads)
                L.HasOptedOutOfEmail=true;
            
            update Leads;
        }
        Catch(Exception e){
            system.debug('Exception occured -->'+E.getMessage()+' at line '+e.getLineNumber());
        }
    }    
    public void finish(Database.BatchableContext bc){
        
    }    
}

 
Ryan MeyerRyan Meyer
I actually copied this code off previous code that was missing it which seemed odd to me as well but was working. I tried adding it to this to no avail.... HasOptedOutOfEmail still false :/ 
Raj VakatiRaj Vakati
Can you add some debug logs and see what is going on 
Abdul KhatriAbdul Khatri
Hi Ryan,

I did not understand what you were trying to say in your last comments?

You mentioned you copied this code from previous some code and it was working there. From the code you mentioning is the entire batch code  or only the following part?
 
for(Lead L : Leads)
                L.HasOptedOutOfEmail=true;
 If that is true then it is high possibility thta this code may be running in before update trigger where you don't need any explicit DML Call. Please clarify what issue you are facing after adding that update Leads statement