You need to sign in to do that
Don't have an account?

Regarding batch apex
Hi,
I am invoking my batch class from a scheduler. In the scheduler i am calling the same batch class twice with different query and parameters one by one like below:-
A_Batch batch1 = new A_Batch('B1', query1);
Database.executeBatch(batch1, 200);
A_Batch batch2 = new A_Batch('B2', query2);
Database.executeBatch(batch2, 200);
Can i assume that my batch2 be called only after batch1 is processed or queue'd?
Thanks.
You cannot execute batch apex from another batch apex class. Also, there is no guarantee when scheduling that your second batch will run after the first is completed.
If you need to chain/serialize batch apex, one way to do it is using the inbound email handler as described in this cookbook entry:
http://developer.force.com/cookbook/recipe/serialize-batch-apex
All Answers
Hi,
Yes you Can call Batch 2 in the finish method of the BATch1
global class batchclass implements Database.Batchable<sObject>,Database.AllowsCallouts{
global String Query{get;set;}
global batchUpdateAcclatlng (String q ){
Query=q;
}
global Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC,List<sObject> scope){
}
global void finish(Database.BatchableContext BC){
//Call any Static method of your Controller or call Batch 2 here
//A_Batch batch2 = new A_Batch('B2', query2);
// Database.executeBatch(batch2, 200);
}
}
Hi,
But i am calling the below methods from a scheduler - is it ok to call batch2 in the finish method - won't it get into a looping condition? Also i would want to know if i call it the way i am doing in scheduler, will it be sequential or parallel calls? As i have few other things to do in the finish method but only when it is called by batch2. Like below:
global class A_Schedule implements Schedulable
{
global void execute (SchedulableContext sc)
{
A_Batch batch1 = new A_Batch('B1', query1);
Database.executeBatch(batch1, 200);
A_Batch batch2 = new A_Batch('B2', query2);
Database.executeBatch(batch2, 200);
}
}
global class A_Batch implements Database.Batchable<sObject>
{
global String cu;
global String query;
global A_Batch(String cust, String queryParam)
{
cu = cust;
query = queryParam;
}
global Database.QueryLocator start(Database.BatchableContext BC)
{
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<sObject> scope)
{
if(cu == 'B1') {// do stuff}
if(cu == 'B2') {// do stuff}
}
global void finish(Database.BatchableContext BC)
{
if(cu == 'B2')
{
// do stuff (but this should happen only after B1 has finished. Can i assume that B2 will call finish after B1 has called it already?
}
}
}
thanks.
You cannot execute batch apex from another batch apex class. Also, there is no guarantee when scheduling that your second batch will run after the first is completed.
If you need to chain/serialize batch apex, one way to do it is using the inbound email handler as described in this cookbook entry:
http://developer.force.com/cookbook/recipe/serialize-batch-apex
Thanks so much. I agree and i tried it on my dev org and found that both the batches were fired together - so aren't done sequentially. This confusion came up because one of the salesforce rep with whom we used to discuss our issues had mailed us that the batches are sequential. Anyways, it is sorted and clear now:). Thanks again!!