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
suresh.csksuresh.csk 

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

Hi.
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..

try{
   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?
cheers
suresh
 
Sagar PareekSagar Pareek
You can use System.abortJob(jobid);

Reference :

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_system.htm#apex_System_System_abortJob
suresh.csksuresh.csk
Hi.
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.

ex:
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);