+ Start a Discussion
Giorgio PorroGiorgio Porro 

Scheduling Batch Jobs

Hello

I created a very simple schedulable interface that executes a number of Batch jobs. 

Is is possible to run additional code only once all batch jobs are complete?
See below.

Thanks a lot in advance
Giorgio

global class iRT_SCHEDULE_Contest implements Schedulable{
   global void execute(SchedulableContext SC) {
 
  List<Contest__c> cList = new List<Contest__c>();  

  cList = [select id from Contest__c]; 
  delete cList;  
 
     // We set the size to 20, 50 etc so that the Batch Jobs reset the governor limits every x iterations
  iRT_BATCH_Contest actuals = new iRT_BATCH_Contest(); 
       database.executebatch(actuals,1); 

>>> CAN I ADD SOME CODE HERE, THAT RUNS ONLY AFTER ALL BATCH JOBS ARE COMPLETE?

              
   }

Best Answer chosen by Giorgio Porro
AshlekhAshlekh
Hi 

When you execute a batch it is not mean that is will execute instantly it depent on resource are available or not. First it will stand in queqe then it will execute.

And you write a any code after executing batch statment then that line will execute and not wait for finish the batch first.

So you can write your code in batch class. When batch finish its work then batch execute it own finish() method in last. So you can wirte yourr line of code in finsih() method.
global class batchClass implements Database.batchable{ 
   global Iterable start(Database.BatchableContext info){ 
       return new CustomAccountIterable(); 
   }     
   global void execute(Database.BatchableContext info, List<Account> scope){
       List<Account> accsToUpdate = new List<Account>();
       for(Account a : scope){ 
           a.Name = 'true'; 
           a.NumberOfEmployees = 70; 
           accsToUpdate.add(a); 
       } 
       update accsToUpdate; 
   }     

   //This function will execute when batch process all records
   global void finish(Database.BatchableContext info){  
      
     // You can wirte your code here   
   
    } 
}
IF it helps you than please mark it as a solution and Like it. ENJOY APEX

All Answers

AshlekhAshlekh
Hi 

When you execute a batch it is not mean that is will execute instantly it depent on resource are available or not. First it will stand in queqe then it will execute.

And you write a any code after executing batch statment then that line will execute and not wait for finish the batch first.

So you can write your code in batch class. When batch finish its work then batch execute it own finish() method in last. So you can wirte yourr line of code in finsih() method.
global class batchClass implements Database.batchable{ 
   global Iterable start(Database.BatchableContext info){ 
       return new CustomAccountIterable(); 
   }     
   global void execute(Database.BatchableContext info, List<Account> scope){
       List<Account> accsToUpdate = new List<Account>();
       for(Account a : scope){ 
           a.Name = 'true'; 
           a.NumberOfEmployees = 70; 
           accsToUpdate.add(a); 
       } 
       update accsToUpdate; 
   }     

   //This function will execute when batch process all records
   global void finish(Database.BatchableContext info){  
      
     // You can wirte your code here   
   
    } 
}
IF it helps you than please mark it as a solution and Like it. ENJOY APEX
This was selected as the best answer
Shri RajShri Raj
As Ashlekh Gera (D-Horse) mentioned, You can write the code in the Finish method. 

Goodluck!