+ Start a Discussion

Break or Stop Batch in start method.Do nothing in start method and exit from batch

I need to stop the batch or I dont want to excute the batch if my condition fails.
Simply I need to exit or break the batch.
In the below code when I tried to do nothing by returning NULL in start method i get error
System.UnexpectedException: Start did not return a valid iterable object..

global Database.QueryLocator start(Database.BatchableContext ctx){
String runBatch = false;
String sql = ' Select Id from Account where city__c ='NewYork' ;
if(runBatch == true)
return Database.getQueryLocator(sql);
return null;

I also checked the https://developer.salesforce.com/forums/ForumsMain?id=906F0000000AffaIAC
but I dont want to return itetrable object in thr try catch like this..

   return iterable object;//this is correct with your existing code
}catch(Exception x){
   System.debug(Error Message);
   return iterable object;
I need to break or do nothing.Is ther any other ways?
Sagar PareekSagar Pareek
You can use System.abortJob(jobid);

Reference :

Thanks for the reply.Yes Yes we can use abortjob.
Other than this any idea ?
Dainel RiveroDainel Rivero
If you do not want to start the batch if a condition fails to return a QueryLocator instead of an iterable, you can return a QueryLocator with a query that you know will not return any records.

String sql;
if(runBatch) {
sql = ' Select Id from Account where city__c ='NewYork' ; // query to use if the condition is met
} else {
sql = 'SELECT Id FROM Account WHERE IsDeleted=true AND IsDeleted=false'; // query that is known in advance that no record will return

return Database.getQueryLocator(sql);