Calling batch from finish of same batch job

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>();

    public Iterable<caseVars> start(Database.BatchableContext BC){
        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;

    public void finish (Database.BatchableContext BC){
        if(remaining > 0){
            StartTheBatch(remaining, cM);

    public static void StartTheBatch(Integer repeats, list<CaseVars> c){
        caseTriggerBatch ct = new caseTriggerBatch(repeats, c);
        Database.executeBatch(ct, 1);


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I think code is kind of simple and self-explanatory. We are making callouts from start method until the governor limit is reached or our records are completed. If governor limit is reached but still records are left then set the chainAnotherBatch boolean to true so that finish method starts the batch again. When you are finished with your records, set the chainAnotherBatch flag to false and finish method exits the batch silently..

Refer to this question and it gives you some clarity on the callouts from batch apex : Number of callouts allowed from batch execute method showing zero?
I hope this solves your requirement.

global class CalloutsFromBatchExample implements Database.Batchable<SObject>,Database.AllowsCallouts,Database.stateful{

    public Boolean chainAnotherbatch = true;

    public void OwnLocalBatchClass(){

    public Iterable<SObject> start(Database.BatchableContext bc){

        Integer totalCallouts = Limits.getLimitCallouts();

        for(Integer i = 0;i<totalCallouts;i++){
            //Make the callout
            //Build the scope object
            //Have a if condition to set the chainAnotherbatch boolean to true or false.            
        return scope;

    public void execute(Database.BatchableContext bc, List<Sobject> scope){

    public void finish(Database.BatchableContext bc){
            Database.executeBatch(new CalloutsFromBatchExample(),1000);
            System.debug('Start method signalled that no need to chain another batch another batch job ');

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x