You need to sign in to do that
Don't have an account?
College Management
Calling Future method from Batch Class ?
Hi , I got a situation where i need to invoke future method from batch class. And i know that it is not possible to do. But there might be some workarounds to achieve it, right? And i dont want to invoke future method using shedule apex as my requirement will hit governor limits of "Total number of schedule jobs exceed". So i tried in below ways, but still fighting with same issue.
My design is
Scenario 1 :
1. Schedule Apex ===> 2. BatchClass (Runs every hour) (Generates a map with "N" records for every batch context, and passes it to Future method in an apex class) ===> 3. Future method ( then it will login to a salesforce account and generates an attachment and sends an email to a corresponding email address) .
Scenario 2 :
Since, It is not possible to invoke future method from batch, i tried implementing an apex class with a normal static method invoking future method and in a batch class, for every context i.e., execute() method, it will invoke a normal method which in turn invokes future method, but still got the same error ( FATAL ERROR: Future method cant be called from future or Batch).
ie.,
global class BatchClass implements Database.batachable<Sobject> {
global Iterable<ScheduledReport__c> start(Database.BatchableContext BC) {
query=[];
return query;
}
global void execute(Database.BatchableContext BC, List<ScheduledReport__c> scope) {
FutureClass.normalMethod(mapRecords);
}
global void finish(Database.BatchableContext BC){
}
}
Future Class:
global class FutureClass {
global static void normalMethod(Map<> mapR) {
futureMethod(mapR);
}
@future(callout=true)
global static void futureMethod(Map<> m) {
//some code;
}
}
Scenario 3 :
Instead of invoking future class method from batch i tried updating list of records and done checkbox value to true and i've implemented after update event trigger to get the all the records in which checkbox values are true and from trigger i tried invoking future method , still didnt work , i got an error ( FATAL ERROR: Future method cant be called from future or Batch).
Please help me out.
Thanks !
My design is
Scenario 1 :
1. Schedule Apex ===> 2. BatchClass (Runs every hour) (Generates a map with "N" records for every batch context, and passes it to Future method in an apex class) ===> 3. Future method ( then it will login to a salesforce account and generates an attachment and sends an email to a corresponding email address) .
Scenario 2 :
Since, It is not possible to invoke future method from batch, i tried implementing an apex class with a normal static method invoking future method and in a batch class, for every context i.e., execute() method, it will invoke a normal method which in turn invokes future method, but still got the same error ( FATAL ERROR: Future method cant be called from future or Batch).
ie.,
global class BatchClass implements Database.batachable<Sobject> {
global Iterable<ScheduledReport__c> start(Database.BatchableContext BC) {
query=[];
return query;
}
global void execute(Database.BatchableContext BC, List<ScheduledReport__c> scope) {
FutureClass.normalMethod(mapRecords);
}
global void finish(Database.BatchableContext BC){
}
}
Future Class:
global class FutureClass {
global static void normalMethod(Map<> mapR) {
futureMethod(mapR);
}
@future(callout=true)
global static void futureMethod(Map<> m) {
//some code;
}
}
Scenario 3 :
Instead of invoking future class method from batch i tried updating list of records and done checkbox value to true and i've implemented after update event trigger to get the all the records in which checkbox values are true and from trigger i tried invoking future method , still didnt work , i got an error ( FATAL ERROR: Future method cant be called from future or Batch).
Please help me out.
Thanks !
For your Ref: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_callouts_timeouts.htm
I suggest give a try putting the callout logic in execute() method as 120 sec is massive time and majority of the services give you response a few seconds.
All Answers
Due to salesforce Limitation, you can't call a future method from inside a batch job and if you have a DML statement inside execute() method which triggers a call to future method it won't work because every run/instance of execute() method is a single transaction and so the trigger execution will be part of that single transaction.
I suggest you leverage the power of batch job to maximum and so you perform all the logic thats inside future method in an instance of execute() - you might need to adjust the batch size if you suspect to hit any governor limits
==> Move the logic that creates mapRecords(make the batch to be Stateful) into Start method of batch job and I'm guessing that key of this map is a salesforce record Id, so at the end of start method return the list of sObjects which has the ids populated and then in execute method you can pickup those records and perform the rest of the logic with them....(if you are using the futureMethodsame then make sure you remove the @future annotation)...to give you a pecise example see below..
The solution may or may not work for you...as it completely depends on your requirements.
Thanks,
Mohammed
Thanks !
For your Ref: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_callouts_timeouts.htm
I suggest give a try putting the callout logic in execute() method as 120 sec is massive time and majority of the services give you response a few seconds.
Thanks !