I have a logic in which sometimes I dont need to execute a batch class execute method. How to achieve this as we cannot pass null or whitespace in query locator

  • What did you try so far ? – SF_user Sep 11 '14 at 14:23

If you can make this decision in the start method, then for the case you don't want anything done you can return zero records like this:

return Database.getQueryLocator([
        select Id from WhicheverObject where Id in ('000000000000000AAA')

as 000000000000000AAA is a valid ID but matches nothing.

  • SELECT Id FROM WhicheverObject WHERE Id = null should be easier on the eyes – Alexander Johannes Sep 12 '14 at 7:26
  • @AlexanderJohannes You are right. – Keith C Sep 12 '14 at 7:58

You can set your limit to zero (just tried this):

public class NullBatch implements Database.Batchable<SObject> {
    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator('select id from user limit 0');

    public void execute(Database.BatchableContext bc, Sobject[] scope) {
        // Do stuff here

    public void finish(Database.BatchableContext bc) {
        // Do stuff here

This will call only the start and finish methods. Of course, you can use if-else statements in the start function, as necessary.


You could create a schedulable class that runs nightly to check your conditions. If those conditions are met then schedule the batch to run immediately. For this, you would need two scheduleable classes, one batch class (which actually can be consolidated into the scheduleable class for the batch) and any helper classes and test classes.

For this solution to work, it all depends on how many records are in your check.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.