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
Rohit Vaidya 3Rohit Vaidya 3 

Batch call from finish

Can an another batch is called form execute and finish method? especially can we call a new batch from finish method in batch apex
Arun Kumar 1141Arun Kumar 1141
Hello Rohit,

The execute method cannot be used to call a batch. Another batch may be called using the start and finish method.

For example, you can call another batch in the finish method using this approach.
 
global void finish(Database.BatchableContext BC) {

 MySecondBatch b = new MySecondBatch(); 

Id batchId = Database.executeBatch(b, 200);
 }


Please mark it as best answer if it helps you.

Thanks
SwethaSwetha (Salesforce Developers) 
HI Rohit,
Based on https://www.biswajeetsamal.com/blog/invoke-batch-apex-from-another-batch-apex/ , Only in batch class finish method, we can call another batch class. The reason for this is that the execute() method will be called many times for a given batch start() and this can get out of hand with too many batches scheduled. If you will call another batch class from batch class execute and start method, then Salesforce will throw runtime error- System.AsyncException: Database.executeBatch cannot be called from a batch start, batch execute, or future method.

Also see this related discussion
https://salesforce.stackexchange.com/questions/53740/system-asyncexception-database-executebatch-cannot-be-called-from-a-batch-start

If this information helps, please mark the answer as best. Thank you