I need to pass the query to the batch class while scheduling it. How could i send the query as a parameter from the schedule class to batch class?

1 Answer 1


You can pass parameters to batch class when instantiating it, for example

String query = 'SELECT Id FROM Account';
SomeBatchClass batch = new SomeBatchClass(query);

and create custom constructor in batch:

public class SomeBatchClass implements Database.batchable<sObject>{
    private String query;
    public SomeBatchClass(String query) {
        this.query = query;

    public Database.QueryLocator start(Database.BatchableContext BC) {
        return Database.query(this.query);
// other code

I edited code due to Felix's comment.


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