To provide a representative example, please consider the code below:
global class SampleJob implements Database.Batchable<sObject> {
Boolean skipExecution;
public SampleJob(Boolean skipExecution) {
this.skipExecution = skipExecution;
}
global Database.QueryLocator start(Database.BatchableContext BC) {
String query = 'SELECT Id, Name, Phone FROM Account';
if(skipExecution) {
return null;
}
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Account> scope) {
for (Account a : scope) {
a.Phone='123';
}
update scope;
}
global void finish(Database.BatchableContext BC){
System.debug('In finish');
}
}
This is only a representative example, in reality my condition for skipping execute will depend on many runtime factors.
If I invoke this code with Database.executeBatch(new SampleJob(false));
, my job should run as usual. However if I pass true
as a parameter instead, I want the job to skip the execute
method and execute finish
directly. This, however gives me a
System.UnexpectedException: Start did not return a valid iterable object.
Is there any way to return a valid 0 size iterable so that the execute method can be skipped?