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
rcherrcher 

Passing a where clause to a batch job dynamically.

Hi,

Is there a way to pass the where clause dynamically to the query which is executed in the database.execute method from developer console.
Best Answer chosen by rcher
Naval Sharma4Naval Sharma4
Hi,

Here is the sample code.
global class BatchExample implements Database.Batchable<sObject>{

   global String WhereClause;
   global String Query;
   global BatchExample(String w){

      Query = 'SELECT Id FROM Account WHERE '+ WhereClause;
   }

   global Database.QueryLocator start(Database.BatchableContext BC){
      return Database.getQueryLocator(query);
   }

   global void execute(Database.BatchableContext BC, List<sObject> scope){
     
    }

   global void finish(Database.BatchableContext BC){
   }
}


Now you can call this batch from developer console.
BatchExample be = new BatchExample('id != null');
Database.executeBatch(be);


Do let me know if you need more help.
 

Regards,
Naval

All Answers

Naval Sharma4Naval Sharma4
Hi,

Here is the sample code.
global class BatchExample implements Database.Batchable<sObject>{

   global String WhereClause;
   global String Query;
   global BatchExample(String w){

      Query = 'SELECT Id FROM Account WHERE '+ WhereClause;
   }

   global Database.QueryLocator start(Database.BatchableContext BC){
      return Database.getQueryLocator(query);
   }

   global void execute(Database.BatchableContext BC, List<sObject> scope){
     
    }

   global void finish(Database.BatchableContext BC){
   }
}


Now you can call this batch from developer console.
BatchExample be = new BatchExample('id != null');
Database.executeBatch(be);


Do let me know if you need more help.
 

Regards,
Naval

This was selected as the best answer
rcherrcher
Thanks Naval, your response helped.