-4

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

2

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

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

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.

4

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