+ Start a Discussion
Ashu sharma 38Ashu sharma 38 

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??
NagendraNagendra (Salesforce Developers) 
Hi Nitish,

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.

Hope this helps.

Kindly mark this as solved if the reply was helpful.

Thanks,
Nagendra
AmanSharma06AmanSharma06

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


 
Shiva RajendranShiva Rajendran
Hi Nithish,
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
public class Queable1 implements Queueable { 
    public void execute(QueueableContext context) {
        System.debug('in Queable1 class');
        ApexFutureMethodClass.method1();
    }

}


Queable 2 : This calls the second future method :
 
public class Queable2 implements Queueable {
public void execute(QueueableContext context) {
        System.debug('in Queable2 class');
        ApexFutureMethodClass.method2();
    }

}

Sample Future Method class with two future methods :
Notice we are scheduling the next queueable class in first future method named method1
public class ApexFutureMethodClass {
@future
public static void method1()
{
    System.debug('in method 1 future');
    Queable2 q2 = new Queable2();
	ID jobID = System.enqueueJob(q2);
	System.debug('Queueable 2 Job Id *** '+jobID);
}
  @future  
public static void method2()
{
    System.debug('in method 2 future');
    
}
    
}

Anonymous class code :
Queable1 q1 = new Queable1();
ID jobID = System.enqueueJob(q1);
System.debug('Queueable 1 Job Id *** '+jobID);

​​​​​​​

Thanks,
Shiva RV
Manjunath S 75Manjunath S 75
Short answer, no.

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.
Deepali KulshresthaDeepali Kulshrestha
Hi Satish,

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