+ Start a Discussion
NitishNitish 

Why we cannot call @future method from Batch Apex but we can call @future from schedulable apex?

I have searched alot but not getting any convincing answer that why we cannot call future from batch apex.
Akshay_DhimanAkshay_Dhiman
Hi Nitish,

All future methods and Batch Classes are Asynchronous and we can not call an Asynchronous call from an Asynchronous call. But to Take control of your asynchronous Apex processes by using the Queueable interface. This interface enables you to add jobs to the queue and monitor them, which is an enhanced way of running your asynchronous Apex code compared to using future methods.

 A future method runs in the background, asynchronously. You can call a future method for executing long-running operations, such as callouts to external Web services or any operation you’d like to run in its own thread, on its own time. You can also make use of future methods to isolate DML operations on different sObject types to prevent the mixed DML error. Each future method is queued and executes when system resources become available. That way, the execution of your code doesn’t have to wait for the completion of a long-running operation. A benefit of using future methods is that some governor limits are higher, such as SOQL query limits and heap size limits

If an @future method could be called from another @future method, one could have a chain of indeterminate length or even create a loop that would extend the execution a transaction indefinitely through a very complex maze of additional execution contexts. Tracing the transaction to completion could become very complex. This would be what's considered an "anti-pattern".

By limiting @future methods to a single call, the execution context is limited to at most two execution contexts. The possibility of creating an infinite loop that can't be resolved doesn't exist. If an @future method could call another @future method, the platform couldn't easily resolve a transaction to completion.

Refer below link

http://www.sfdcstuff.com/2016/10/asynchronous-apex-apex-scheduler-batch.html

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_invoking_future_methods.html

https://resources.docs.salesforce.com/194/latest/en-us/sfdc/pdf/salesforce_async_processing.pdf

Hope it helps you.


Thanks
Akshay
NitishNitish
Thanks Akshay for your reply As you have mentioned we can not call an Asynchronous call from an Asynchronous call then we can call future from Schedulable but not from batch apex. 
Akshay_DhimanAkshay_Dhiman
Yes Nitish, Please make it best answer so it can help others too.