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
usersfdc21usersfdc21 

How to execute batch classes one after the other?

I have got two batch classes written for Integrating from SF to ActiveCampaign and need to execute one after the other.
May I know how to do it?
Best Answer chosen by usersfdc21
Jai ChaturvediJai Chaturvedi
Hi,

1. First Batch Class
global class AccountBatch implements Database.Batchable<sObject>{

    global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator('SELECT id FROM Account');
    }

    global void execute(Database.BatchableContext BC, List<sObject> scope){
        // do your Account updating here
    }

    global void finish(Database.BatchableContext BC){
           //Call 2nd batch from here.
            System.schedule('Batch 2', cron, new BatchScheduler2());
    }

}

2. Second batch class.
global class ContactBatch implements Database.Batchable<sObject>{

    global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator('SELECT id FROM Contact');
    }

    global void execute(Database.BatchableContext BC, List<sObject> scope){
        // do your Contact updating here
    }

    global void finish(Database.BatchableContext BC){

    }

}

3. First Scheduler class. This will call 1st Batch class.
global class BatchScheduler implements Schedulable {
    global void execute(SchedulableContext sc){
         database.executeBatch(new AccountBatch());
    }
}

4. Second Scheduler class. This will call 2nd Batch class.
global class BatchScheduler2 implements Schedulable {
    global void execute(SchedulableContext sc){
         database.executeBatch(new ContactBatch());
    }
}

I think this will solve your problem. 


Thanks
Jai

All Answers

Jai ChaturvediJai Chaturvedi
Hi,

1. First Batch Class
global class AccountBatch implements Database.Batchable<sObject>{

    global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator('SELECT id FROM Account');
    }

    global void execute(Database.BatchableContext BC, List<sObject> scope){
        // do your Account updating here
    }

    global void finish(Database.BatchableContext BC){
           //Call 2nd batch from here.
            System.schedule('Batch 2', cron, new BatchScheduler2());
    }

}

2. Second batch class.
global class ContactBatch implements Database.Batchable<sObject>{

    global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator('SELECT id FROM Contact');
    }

    global void execute(Database.BatchableContext BC, List<sObject> scope){
        // do your Contact updating here
    }

    global void finish(Database.BatchableContext BC){

    }

}

3. First Scheduler class. This will call 1st Batch class.
global class BatchScheduler implements Schedulable {
    global void execute(SchedulableContext sc){
         database.executeBatch(new AccountBatch());
    }
}

4. Second Scheduler class. This will call 2nd Batch class.
global class BatchScheduler2 implements Schedulable {
    global void execute(SchedulableContext sc){
         database.executeBatch(new ContactBatch());
    }
}

I think this will solve your problem. 


Thanks
Jai
This was selected as the best answer
usersfdc21usersfdc21
Hi Jai,

Thank you very much for your quick response and detail explanation.
I am sorry for late reply.

My problem got solved with little modification from your code as below:

global void finish(Database.BatchableContext BC) {
            Database.executeBatch(new BatchClassB(constructors),2000);
    }