+ Start a Discussion
surendra challepalelsurendra challepalel 

Is it possible to call batch class from one more batch class? explain example?

pooja biswaspooja biswas
Hi surendra

Yes, it is possible that purely depends upon the scenario.

assume u have batch apex 1 and batch apex2. In finish nethod of batch apex1 u can call instance of batch apex2. so this way only when batch apex1 fully completes the batch apex2 starts.

Scenario: Assume u have loaded 1000 account objecr records using data loader into account and now for every account u need to create an
                contact & every account should also have an opportutnity.

global class CreateContactsUnderAccountBatchApex Implements Database.Batchable<Sobject>
{
   
   global Database.QueryLocator start(Database.BatchableContext BC)
   {
     string obj='%Cathay Pacific%';
     string query='select Name from account where Name LIKE :obj Order BY Name';
     return Database.getQueryLocator(query);
   }
    
   global void execute(Database.BatchableContext BC,List<Account> scope)
   {    
      //loop through the account and create contact records.
      List<Contact> NewconList=new List<Contact>();
     
      forAccount acc:scope)
      {
         Contact con=new Contact();
         con.LastName=acc.Name;
         con.Phone='9898989899';
         con.AccountID=acc.ID;
        
         NewconList.add(con);
      }
    }
     global void finish(Database.BatchableContext BC)
   {
    //code to send an email

    //schedule the next batch apex to start after 10 minutes only when the first one completes--write the code here and call instance of Jobrunner
    //class
    Database.executeBatch('batch job1','cron_expr,new JobRunner());
   }
}

global class CreateOpportunitiesUnderAccountBatchApex Implements Database.Batchable<Sobject>
{
   List<Opportunity> NewOpporList=new List<Opportunity>();
   
   global Database.QueryLocator start(Database.BatchableContext BC)
   {
     string obj='%Cathay Pacific%';
     string query='select AccountID,LastName from contact where LastName LIKE :obj Order BY Name';
     return Database.getQueryLocator(query);
   }
  
   global void execute(Database.BatchableContext BC,List<Contact> scope)
   {    
     for(Contact con:scope)
      {
         Opportunity o=new Opportunity();
        
         o.Name=con.LastName+'Oppor';
                 
         o.CloseDate=Date.today().addDays(5);  
        
         o.Amount=1000000;
        
         o.StageName='Prospecting';
        
         o.AccountID=con.AccountID;
        
         NewOpporList.add(o);
      }
   }}
  
   global void finish(Database.BatchableContext BC)
   {
     //code to send email
  }}

global class JobRunner Implements Schedulable
{
   global void execute(SchedulableContext ctx)
   {
     ID batchJoOpporJobID = Database.executeBatch(new CreateOpportunitiesUnderAccountBatchApex(),200);
   }
}

Note: when u nested batch apex u have to make sure that the second batch apex job starts only after the first one completes.

Hope this helps.

Mark the answer if this helps ypou.

Thanks
Pooja Biswas