Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.
error

We made a wrong turn. Try again.

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

 
3 answers
  1. Feb 28, 2020, 6:04 PM
    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);

0/9000