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
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.