Skip to main content
added 2 characters in body
Source Link
Chris Duncombe
  • 24.3k
  • 13
  • 77
  • 116

One solution that was mentioned in athe comments is to call another Batch class from the finish method of another Batch class. This is actually somewhat new functionality (I want to say Spring '13, but not completely sure), but I have used this in quite a few batch classes and it works great. See a sample below

In my Scheduled Class

global void execute(SchedulableContext sc) {
        
    BatchClass1 Batch1 = new BatchClass1();
    ID batchprocessid = Database.executeBatch(Batch1 ,50);          
}

My Batch1 Batch Class

global class BatchClass1 implements Database.Batchable<sObject>{

    global final string query;

    global BatchClass1(){
        query = 'Select Id, Name From MyObject__c';     
    } 
    
    global Database.QueryLocator start(Database.BatchableContext BC){

        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC,List<sObject> scope){
        List<MyObject__c> myObjectsToUpdate = new List<MyObject__c>();
        
        for(Sobject s : scope){
            MyObject__c o = (MyObject__c) s;
            // Some Logic and/or actions on your objects
        }  
        
        try{
            update myObjectsToUpdate ;
        }catch (Exception e) {  
            //Your Error trapping   
        }
    }
    
    /*
    
    In your finish method, you call your next batch class.  You can chain
    these batch classes as many times as you like.  In the Batch2 finish method,
    can call Batch3 and so on.
    */
    global void finish(Database.BatchableContext BC){
        BatchClass2 Batch2 = new BatchClass2();
        ID batchprocessid = Database.executeBatch(Batch2 ,50);  
    }
}

One solution that was mentioned in a comments is to call another Batch class from the finish method of another Batch class. This is actually somewhat new functionality (I want to say Spring '13, but not completely sure), but I have used this in quite a few batch classes and it works great. See a sample below

In my Scheduled Class

global void execute(SchedulableContext sc) {
        
    BatchClass1 Batch1 = new BatchClass1();
    ID batchprocessid = Database.executeBatch(Batch1 ,50);          
}

My Batch1 Batch Class

global class BatchClass1 implements Database.Batchable<sObject>{

    global final string query;

    global BatchClass1(){
        query = 'Select Id, Name From MyObject__c';     
    } 
    
    global Database.QueryLocator start(Database.BatchableContext BC){

        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC,List<sObject> scope){
        List<MyObject__c> myObjectsToUpdate = new List<MyObject__c>();
        
        for(Sobject s : scope){
            MyObject__c o = (MyObject__c) s;
            // Some Logic and/or actions on your objects
        }  
        
        try{
            update myObjectsToUpdate ;
        }catch (Exception e) {  
            //Your Error trapping   
        }
    }
    
    /*
    
    In your finish method, you call your next batch class.  You can chain
    these batch classes as many times as you like.  In the Batch2 finish method,
    can call Batch3 and so on.
    */
    global void finish(Database.BatchableContext BC){
        BatchClass2 Batch2 = new BatchClass2();
        ID batchprocessid = Database.executeBatch(Batch2 ,50);  
    }
}

One solution that was mentioned in the comments is to call another Batch class from the finish method of another Batch class. This is actually somewhat new functionality (I want to say Spring '13, but not completely sure), but I have used this in quite a few batch classes and it works great. See a sample below

In my Scheduled Class

global void execute(SchedulableContext sc) {
        
    BatchClass1 Batch1 = new BatchClass1();
    ID batchprocessid = Database.executeBatch(Batch1 ,50);          
}

My Batch1 Batch Class

global class BatchClass1 implements Database.Batchable<sObject>{

    global final string query;

    global BatchClass1(){
        query = 'Select Id, Name From MyObject__c';     
    } 
    
    global Database.QueryLocator start(Database.BatchableContext BC){

        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC,List<sObject> scope){
        List<MyObject__c> myObjectsToUpdate = new List<MyObject__c>();
        
        for(Sobject s : scope){
            MyObject__c o = (MyObject__c) s;
            // Some Logic and/or actions on your objects
        }  
        
        try{
            update myObjectsToUpdate ;
        }catch (Exception e) {  
            //Your Error trapping   
        }
    }
    
    /*
    
    In your finish method, you call your next batch class.  You can chain
    these batch classes as many times as you like.  In the Batch2 finish method,
    can call Batch3 and so on.
    */
    global void finish(Database.BatchableContext BC){
        BatchClass2 Batch2 = new BatchClass2();
        ID batchprocessid = Database.executeBatch(Batch2 ,50);  
    }
}
Source Link
Chris Duncombe
  • 24.3k
  • 13
  • 77
  • 116

One solution that was mentioned in a comments is to call another Batch class from the finish method of another Batch class. This is actually somewhat new functionality (I want to say Spring '13, but not completely sure), but I have used this in quite a few batch classes and it works great. See a sample below

In my Scheduled Class

global void execute(SchedulableContext sc) {
        
    BatchClass1 Batch1 = new BatchClass1();
    ID batchprocessid = Database.executeBatch(Batch1 ,50);          
}

My Batch1 Batch Class

global class BatchClass1 implements Database.Batchable<sObject>{

    global final string query;

    global BatchClass1(){
        query = 'Select Id, Name From MyObject__c';     
    } 
    
    global Database.QueryLocator start(Database.BatchableContext BC){

        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC,List<sObject> scope){
        List<MyObject__c> myObjectsToUpdate = new List<MyObject__c>();
        
        for(Sobject s : scope){
            MyObject__c o = (MyObject__c) s;
            // Some Logic and/or actions on your objects
        }  
        
        try{
            update myObjectsToUpdate ;
        }catch (Exception e) {  
            //Your Error trapping   
        }
    }
    
    /*
    
    In your finish method, you call your next batch class.  You can chain
    these batch classes as many times as you like.  In the Batch2 finish method,
    can call Batch3 and so on.
    */
    global void finish(Database.BatchableContext BC){
        BatchClass2 Batch2 = new BatchClass2();
        ID batchprocessid = Database.executeBatch(Batch2 ,50);  
    }
}