Edit: Have edited the title in the hopes that anyone in the future looking for help on a related topic can find this easily. Here is a great way to implement polymophism with batch classes: https://salesforce.stackexchange.com/a/156010/40851
I'm writing a few job classes: Job1, Job2, and so on.
global class Job1 implements Database.Batchable<sObject>, Database.Stateful {
private String query;
global Job1(String query) {
this.query = query;
}
global void setQuery(String query) {
this.query = query;
}
global String getQuery() {
return query;
}
global Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc, List<Account> scope){
// process each batch of records
}
global void finish(Database.BatchableContext bc){
// other logic goes here
}
}
I've created 5 such classes like this.
Next, I want to be able to use polymorphism and instantiate job objects as such:
Database.Batchable<sObject> j1 = new Job1();
Now, I'll do j1.setQuery('SELECT Name from Account');
However, doing this results in:
Method does not exist or incorrect signature: [Database.Batchable<SObject>].setQuery(String)
What am I doing wrong?
Job1 j1 = new Job1();
instead ofDatabase.Batchable<sObject> j1 = new Job1();
? I feel like if thesetQuery
method is only defined on the Job1 class, you'd have to be calling the method on an instance of a Job1