function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
debprakash shawdebprakash shaw 

how to call same batch class inside batch class in finish method

I want to call the same batch class in the same batch. Please give me the syntax of how to call in the finish method.

Thanks in Advance
Best Answer chosen by debprakash shaw
mukesh guptamukesh gupta
Hi Debprakash,

Please follow below code:-
 
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){
            Database.insert(scope); 
    }

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

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi debprakash,

Refer the below link have similar kind of ask.
https://salesforce.stackexchange.com/questions/88890/calling-batch-from-finish-of-same-batch-job

If this information helps, Please mark it as best answer.

Thanks!!
mukesh guptamukesh gupta
Hi Debprakash,

Please follow below code:-
 
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){
            Database.insert(scope); 
    }

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

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
This was selected as the best answer
Cleveland WillisCleveland Willis
Where can I get live results of matches on sports sites with codes? You can find live match results and league standings of teams in live koora (https://livekoora.com/)