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

how to call future into another future method
Hi All,
I want to know the basic resons for below point:
1.can we call future method into batch apex,if not why??
2.can we call futute method into anothrt future method??
I want to know the basic resons for below point:
1.can we call future method into batch apex,if not why??
2.can we call futute method into anothrt future method??
You can`t call future methods from Batch context.
I believe that workarounds for this depends on why you are using future methods in your code.
If you need to do a callout then you can remove @future annotation and do it directly from your Batch but you might want to reduce Batch size because of limits. Also reducing Batch size might help if you are using @future to have more resources for some methods.
If your Batch is triggering some code that does future calls you might want to add conditions to that code like
if(System.isFuture() || System.isBatch()){ //Your logic here } else { //Mark records with some NeedToProcess__c = true; and then process them in another Batch //moving future logic to that Batch. Or just move your future logic to your Batch and do //nothing in else case. }
This thread might help you as you can schedule some Schedulable class from your Batch which can invoke future methods.
If you try to invoke a future method from Batch/Future you will get the below error -
FATAL ERROR: Future method cant be called from future or Batch
But noteworthy point here is each execute method of batch apex is executed in asynchronously using its own resources and governor limits as an @future methods. So I would suggest you to give a try by putting logic in execute() method
Reference - https://resources.docs.salesforce.com/194/latest/en-us/sfdc/pdf/salesforce_async_processing.pdf
For calling one future method from another future method please refer to below link.
- https://salesforce.stackexchange.com/questions/75779/future-method-calling-another-future-method
Hope this helps.Kindly mark this as solved if the reply was helpful.
Thanks,
Nagendra
Changing sequence of your questions to answer this :
1.can we call future method into another future method??
Ans : No, we can't call future method from any other furture method.
2.can we call future method into batch apex,if not why??
No we can't call future from batch apex, reason being is each invocation of execute method and finish method are independent threads( in other words future type process) and because salesforce not allow callling of future from another future, they had to block future from batch apex too.
See below errors from Setup-Job-Apex Jobs UI:
When future method called from execute method of batch apex:
First error:Future method cannot be called from a future or batch method:
When only ONE future method called from finish method of batch apex:
Too many future calls: 1
You can consider using Queuable interface. You can get it done.
For sample you can use this snippet :
Queable1 : This calls the first future method
Queable 2 : This calls the second future method :
Sample Future Method class with two future methods :
Notice we are scheduling the next queueable class in first future method named method1
Anonymous class code :
Thanks,
Shiva RV
The Future methods get queued as a Async job on the Salesforce Flex queue as a separate entity. That's why you cannot have a non-void return for these methods. They are also always supposed to be static, as their state can be independent of the transaction that called the future method.
Now, as to WHY future cannot be called from a Batch context, is because both of the jobs will be queued separately, in different threads on the appserver. So to optimise resource allocation, Salesforce does not allow a future method from a batch class, so that a single org's jobs do not fill up the flex queue.
You can't call another future method from a future method. As per Salesforce documentation,
You can't call a method annotated with the future from a method that also has the future annotation. Nor can you call a trigger from an annotated method that calls another annotated method.
If you do not have a dependency between f1 & f2, you can call both methods one by one from the original class.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha
I was able to call future method in another but of same class, please help me understand
public class BasicFutureMethod {
public void callFuturemethods(){
Futuremethod_1();
Futuremethod_2();
List<Integer> intList = new List<Integer>{1,2};
Futuremethod_3(intList);
}
@future
public static void Futuremethod_1(){
Futuremethod_2();
system.debug('In Futuremethod_1');
}
@future
public static void Futuremethod_2(){
system.debug('In Futuremethod_2');
}
}