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
alaschgarialaschgari 

How to implement a batch queue?

Hey folks,

 

My customer has an org where batches are started regularly throughout the day. In order to prevent getting the "5 batches limit" error, my primary solution would be to implement something like a batch queue. Is this a) the best solution and b) if yes, how can I implement it?

 

Cheers

Josh :-)

Best Answer chosen by Admin (Salesforce Developers) 
Vinit_KumarVinit_Kumar

Hi Josh,

 

You need to create a schedulable class which would invok multiple batch class one after another.The sample code is below :-

 

//declaration of scheduler class
global class ScheduleMultipleBatchJobs implements schedulable
    {
    //method to implement Apex schedulers
    global void execute(SchedulableContext ct)
        {
            BatchDeletion BD = new BatchDeletion();
            Database.executebatch(BD); // Invoking first batch
            
            UpdateAccountFields UP = new UpdateAccountFields();
            Database.executebatch(UP); //invoking second batch
        
        }
    }

 

Here,I am scheduling two batch classes (BatchDeletion and UpdateAccountFields)  in a sinle schedulable class,the 2nd batch class woul start after the finish of first and so on,hence you would not be getting concurrent batch error as the the batches would run after another.