You need to sign in to do that
Don't have an account?
TheRealist
How to call another batch Apex from one batch apex?
How to call another batch apex from one batch apex? i am providing 2 sample batch apexes just for testing purpose, or else can some one provide me a sample batch apex in which another batch will be called
Batch apex 1
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){
}
}
called with below code
Batchleadconverted b=new BatchLeadConverted();
database.executebatch(b,2);
Batch Apex 2
public class accno implements database.Batchable<sobject>
{
integer i=0;
public string query='select id,name from account';
public Database.QueryLocator start(Database.BatchableContext bc)
{
return database.getQueryLocator(query);
}
public void execute(database.BatchableContext bc, list<account> acc)
{
for(account a:acc)
{
a.name=a.name+i;i++;
}
update acc;
}
public void finish(database.BatchableContext bc)
{
messaging.SingleEmailMessage ms=New messaging.SingleEmailMessage();
string[] mailme=new string[] {'xxxxxx@gmail.com'};
ms.settoAddresses(mailme);
ms.setsubject ('record update');
ms.setPlainTextBody('ExampleText');
messaging.sendEmail(new messaging.SingleEmailMessage[]{ms});
}
}
call with below code
accno a=new accno ();
database.executebatch(a)
Batch apex 1
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){
}
}
called with below code
Batchleadconverted b=new BatchLeadConverted();
database.executebatch(b,2);
Batch Apex 2
public class accno implements database.Batchable<sobject>
{
integer i=0;
public string query='select id,name from account';
public Database.QueryLocator start(Database.BatchableContext bc)
{
return database.getQueryLocator(query);
}
public void execute(database.BatchableContext bc, list<account> acc)
{
for(account a:acc)
{
a.name=a.name+i;i++;
}
update acc;
}
public void finish(database.BatchableContext bc)
{
messaging.SingleEmailMessage ms=New messaging.SingleEmailMessage();
string[] mailme=new string[] {'xxxxxx@gmail.com'};
ms.settoAddresses(mailme);
ms.setsubject ('record update');
ms.setPlainTextBody('ExampleText');
messaging.sendEmail(new messaging.SingleEmailMessage[]{ms});
}
}
call with below code
accno a=new accno ();
database.executebatch(a)
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
The term for this I've seen used is "batch chaining". You can kick off the 2nd batch process within the finish method - I've done this succesfully. Here's one example:
global void finish(Database.BatchableContext BC)
{
Batchleadconverted b=new BatchLeadConverted();
Id batchId = Database.executeBatch(b, 2);
}
Is this what you were looking for? If so, please mark this as the answer. FYI you can also create a scheduable class for this batch and schedule the batch later on vs executing immediately.
Thanks,
Bronco
You can call the other batch class either in start method or in finish method as per your requirment .
You need to create the instance of the second batch class and call the execute method in Database class either passing insance of batch class or instance of batch class and batch size .
SecondBatch bat=new SecondBatch ();
Database.executeBatch(bat, 100);
or
Database.executeBatch(bat);
Let me know if it helps .
Thanks
Manoj
Call the another batch class in the finish method.