Every Month, we have to reset a flag(of type boolean) on Custom Objects A,B and C. For this, I have written one batch class per object.
ObjAResetBatch.cls
public class ObjAResetBatch implements Database.Batchable<SObject> {
public Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator('SELECT Id,Flag__c FROM A__c WHERE Flag__c = TRUE');
}
public void execute(Database.BatchableContext BC, List<SObject> scope){
List<A__c> objAList = (List<A__c>)scope;
for(A__c aObj: objAList){
aObj.Flag__c = FALSE;
}
update objAList;
}
public void finish(Database.BatchableContext BC){
}
}
There are two other batch classes that are almost same as above but work on Custom Objects B & C.
Is it possible to combine these three batches into single batch?
Best Answer
I would think you can utilize something by introducing a constructor to accept the query and accordingly execute it.
Your common batch class will then look like:
And then, you execute the batches accordingly:
Note that I haven't really gone into the details of effectively writing all conditions, but this should be a step towards what you are trying to achieve.