Trying to implement Dan Appleman solution to call batch job from finish method of another batch job. Bu the execute method is continously looping. Can someone suggest me on ways to improve my batch class to achieve what Dan got explained in his solution (calling a method in the finish of batch job which again calls the same batch job when there are still records pending). Thanks
public class caseTriggerBatch implements Database.Batchable<caseVars>, Database.AllowsCallouts, Database.Stateful {
public integer remaining;
public string jsonString;
public list<CaseVars> cM;
public caseTriggerBatch(Integer count, list<CaseVars> cTest){
remaining = count-1;
cM = cTest;
//cM = new List<CaseVars>();
//cM.add(cTest[remaining]);
}
public Iterable<caseVars> start(Database.BatchableContext BC){
system.debug('starting'+remaining);
return cM;
}
public void execute (Database.BatchableContext BC, List<CaseVars> cList){
list<caseVars> cV = new list<caseVars>();
for(caseVars c: cList){
CaseVars caseV = new CaseVars();
caseV.caseId = c.caseId;
caseV.caseNumber = c.caseNumber;
caseV.origin = c.origin;
cV.add(caseV);
}
system.debug(cV);
}
public void finish (Database.BatchableContext BC){
remaining--;
system.debug('exiting'+remaining);
if(remaining > 0){
StartTheBatch(remaining, cM);
system.debug('last'+remaining);
}
}
public static void StartTheBatch(Integer repeats, list<CaseVars> c){
caseTriggerBatch ct = new caseTriggerBatch(repeats, c);
Database.executeBatch(ct, 1);
}
}
async apex pattern
? Yourexecute(..)
method does nothing useful. I don't see how items in the initial list are ever removed