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
hiteshwar marnihiteshwar marni 

batch apex calling

Can we call one batch apex from another batch apex?

Thanks
Preya VaishnaviPreya Vaishnavi
yep you can call another batch apex in finish method of the batch class
buggs sfdcbuggs sfdc

HI,

To call another batch class from a batch class,  
Call the another batch class in the finish method.
As execute method being called many times but Start and finish method only once.
So once your main batch is completed and the finish method is called and then it will call the another batch. 
 
global void finish(Database.BatchableContext BC)
{
    Database.executeBatch(new MyBatch());
}

Your Example::

DataBase.ExecuteBatch(new BatchLeadConverted(),2);

public class BatchLeadConverted implements database.Batchable<sobject>
{
    list<lead> llist=new list<lead>();
    public string query='select id,name,status from lead';
    public string flag='Closed - Not Converted';
    
    public database.querylocator start(database.BatchableContext bc){
       return database.getQueryLocator(query); 
  }
    public void execute(database.BatchableContext bc,list<lead> le){
        for(lead l:le){
            if(l.status==flag)
            {
                llist.add(l);
            }
                
        }

      delete llist;            
      database.emptyRecycleBin(llist); 
    }
    public void finish(database.BatchableContext bc){
           Database.executeBatch(new new accno ());
        
    }
 }


You will get more details..... Please refer below URL's
1. https://success.salesforce.com/ideaview?id=08730000000KNzxAAG
2. https://developer.salesforce.com/forums?id=906F00000009295IAA