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
sfdeveloper9sfdeveloper9 

Batch apex start method return

I have a need where I need to check some condition in the start method of a batch apex and based on that database.querylocator query should execute. what is the best way to exit start without running database.queryLocator query. I tried returning null but it throws exception.
The workaround I have for now is putting a limit 0 for the query and executing the database.querylocator like below
if(some condition){
     //I want to exit the start method here and continue to the finish method.
       return database.queryLocator ('select id from account limit 0'); 
}
else{
       return database.queryLocator ('select id from account');
}

Please let me know if there is any better way to achieve this.
Tejpal KumawatTejpal Kumawat
Hello sfdeveloper9,

If you entered in the batch then you can't exit start without running database.queryLocator query.

If you want to this then run batch(Incontroller/ schedular) on condition bases.
if(!some condition){
       //run your batch in your controller or schedular
}

If this answers your question then hit Like and mark it as solution!