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
Gautam_KasukhelaGautam_Kasukhela 

Scheduled Apex Batch Job Issue

Hello All,
    I have an Apex class (SendEmailCommunications) that implements "Database.Batchable<sObject>,Schedulable" and has the 'start', 'execute' & 'finish' methods.
global Database.QueryLocator start(Database.BatchableContext bc)
{
return Database.getQueryLocator( <My Query>);
}

global void execute(Database.BatchableContext bc, List<My_Custom_Object> scope){
        System.debug('Scope Size is: '+scope.size());
 //more logic
}

global void finish(Database.BatchableContext bc)
    {
        System.debug('Sent emails');
    }
I have used the 'Apex Scheduler' in Setup to schedule this class to run at a designated time. As per the logs, the batch job starts and finshes without running any logic. Even the first system.debug statement in my execute method does not get logged.
User-added image
What am I missing here?
 
Best Answer chosen by Gautam_Kasukhela
Gautam_KasukhelaGautam_Kasukhela
Figured out the issue. As the class was implementing both Batchable and Schedulable, there was a 2nd execute method in the same file (this was part of the Schedulable) and this method was empty. As the Apex class was scheduled, the first method to run will be the execute of the Schedulable. As the method was empty and no implementation was done here, the code was not logging anything.

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Gautam,

Greetings to you!

Make sure the start method fetches the required records to process in execute method. Execute will not be called unless at least one non-null item is available for processing.

Also, please refer to the below links which might help you further.

https://salesforce.stackexchange.com/questions/112821/batch-apex-execute-method-not-invoked

https://salesforce.stackexchange.com/questions/98874/execute-method-in-apex-batch-never-called

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm#!

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Gautam_KasukhelaGautam_Kasukhela
Thanks Khan. The query that I have in the class does fetch records (fetches 26 records when i executed the same in anonymous window). 
Gautam_KasukhelaGautam_Kasukhela
My code does not even enter the start () method. 
Gautam_KasukhelaGautam_Kasukhela
Figured out the issue. As the class was implementing both Batchable and Schedulable, there was a 2nd execute method in the same file (this was part of the Schedulable) and this method was empty. As the Apex class was scheduled, the first method to run will be the execute of the Schedulable. As the method was empty and no implementation was done here, the code was not logging anything.
This was selected as the best answer