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
Mat KwokMat Kwok 

Yo dawg, I heard you like Batch Apex

Hi all,

 

Is it possible to execute another batch apex job in the Finish method of a batch class?

 

 

global class SomeBatchJob implements Schedulable, Database.Batchable<SObject> 
{
	
    global void execute(SchedulableContext ctx)
    {       
        Database.executeBatch(new SomeBatchJob(),1);              
    }
    
    global Database.QueryLocator start(Database.BatchableContext ctx)
    {
        // Some query
    }
    
    global void execute(Database.BatchableContext ctx, List<SObject> items)
    {
        // Operations
    }	    
    
    global void finish(Database.BatchableContext ctx)
    {  
         Database.executeBatch(new SomeOtherBatchJob(),1);
    }   
}

 In essence, I want to be able to daisy-chain 3 schedulable batch jobs, instead of scheduling 3 separate APEX classes. Is this allowed?

 

Best Answer chosen by Admin (Salesforce Developers) 
Cory CowgillCory Cowgill

I believe a work around is that you can fire Scheduled Apex in the Finish Method.

 

In the Schedule Apex you could tell it to fire some Batch Apex.

All Answers

dmchengdmcheng

I don't think it can be done - you'll have to keep them separate.

Mat KwokMat Kwok

That's what I thought. I tried it and it didn't work. Thanks for confirming.

Cory CowgillCory Cowgill

I believe a work around is that you can fire Scheduled Apex in the Finish Method.

 

In the Schedule Apex you could tell it to fire some Batch Apex.

This was selected as the best answer