Skip to main content
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.
1 resposta
  1. 27 de fev. de 2015, 02:44
    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!