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
vickySFDCvickySFDC 

How to run Multiple Batch class one after one completeing?Give Examples...

Hi All,

 

I have 4 batch class and execute one by one like

 

batch1,batch2,batch 3,Batch4 

 

my requirement is starting the  Batch1 class  after this one completed then I have to start next batch 2 then batch3 and batch 4.

 

How to handle this situtation in my batch class? Give me some scenario......

 

 

Thanks,

 

 

Vicky

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Subhash GarhwalSubhash Garhwal

Hi Vicky.

 

You can do this by execute next batch in first batch's finish method

 

Like

Batch 1 finish method

Database.executeBatch(new Batch 2(),200);

 

Batch 2 finish method
Database.executeBatch(new Batch 3(),200);

 

Batch 3 finish method
Database.executeBatch(new Batch 4(),200);

 

By this way when you execute Batch 1 than once it finish batch2 start and after batch2, batch3 start and after batch3 batch4 will start.

 

Thanks

 

Hit the Kudos button (star)  and Mark as solution if it post helps you

All Answers

Subhash GarhwalSubhash Garhwal

Hi Vicky.

 

You can do this by execute next batch in first batch's finish method

 

Like

Batch 1 finish method

Database.executeBatch(new Batch 2(),200);

 

Batch 2 finish method
Database.executeBatch(new Batch 3(),200);

 

Batch 3 finish method
Database.executeBatch(new Batch 4(),200);

 

By this way when you execute Batch 1 than once it finish batch2 start and after batch2, batch3 start and after batch3 batch4 will start.

 

Thanks

 

Hit the Kudos button (star)  and Mark as solution if it post helps you

This was selected as the best answer
GlynAGlynA

In your finish() method, schedule the next batch.  You can compute a start time from the current time like this:

 

    DateTime scheduledTime = DateTime.now().addSeconds( 2 );
    String timeString = scheduledTime.format( 'yyyy-MM-dd HH:mm:ss' );
    String cronString = scheduledTime.format( 's m H d M ? yyyy' );
    System.schedule( 'Batch2 - ' + timeString + ' (' + Math.random() + ')', cronString, new Batch2() );

 where Batch2 is your schedulable class.  This schedules the 2nd batch for 2 seconds after the first batch finishes.  I put the time string in the scheduled job name (along with a random number) because the scheduled job name must be unique.

 

If this helps, please mark it as the solution, and give kudos (click on the star) if you think I deserve them.  Thanks!

 

-Glyn

vickySFDCvickySFDC

Hi Subash,

 

Thanks  Quik reply for both .

 

In finish mehtod of batch1 class  Can mention  the Database.executebatch() method inside scope.

 

 global void finish(Database.BatchableContext BC)
    {

      Database.executebatch(new Batch2(),200)://like this we can mention the Code itself.


    }

 

 

Thanks,

 

Vicky

Subhash GarhwalSubhash Garhwal
Yes
Jyoti ArusiaJyoti Arusia
Hi Gyln,
Thanks a lot!! looking for exactly this type of information for my case.

-Jyoti
Vatsal Pancholi 6Vatsal Pancholi 6
Hi All,

What can we do if the apex code is written in a Schedulable Context and not in Batchable context, where we don't have the finish method?

My scenario is, I have four Schedulers. I have created 2 future methods in Scheduler no. 2, which is calling Scheduler 3 and 4. Now I want to trigger Scheduler no. 2 on completion of Scheduler no.1. Since Scheduler 2 has two future methods with two Scheduler calls, I can't call Scheduler in the future method for Scheduler no. 1. Basically, all the Scheduler should run back to back when I run Scheduler no.1 from DEV console.

So how do I CALL and NOT SCHEDULE, Scheduler no. 2 on completion of Scheduler no. 1?


P.S. - I've mentioned "Batch" as "Scheduler" since they are 2 different concepts in SFDC.

Thanks,
Vatsal Pancholi