function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Saravana RavikumarSaravana Ravikumar 

What is the workaround if I we can't call a future method from another future method ?

Arun Kumar 1141Arun Kumar 1141
Hi Ravikumar,

In Salesforce, it is not possible to directly call a future method from another future method. This restriction exists because each future method is designed to execute asynchronously, and calling one future method from another could potentially lead to a deadlock or other performance issues.
However, if you need to perform some logic sequentially or pass data from one future method to another, there are a few workarounds you can consider:

1. Chaining future methods: Instead of directly calling a future method from another, you can chain them by using the @future(callout=true) annotation. The first future method can initiate the second future method through a callout, passing any necessary data as parameters.
2. Queueable interface: You can implement the Queueable interface in a separate class and use the System.enqueueJob method to add the job to the Apex job queue. This allows you to chain multiple Queueable jobs together, achieving sequential execution. You can pass data between jobs using instance variables or by serializing and deserializing objects.
3. Synchronous method invocation: If your business requirements allow it, you can convert your future methods into synchronous methods. This enables you to call one method from another directly. However, keep in mind that synchronous methods may impact performance and should be used judiciously.

If this helps you please mark it as a best answer
Thanks!
SwethaSwetha (Salesforce Developers) 
HI Ravi,

What is your usecase of your scenario?

Basically, it is not possible to call a future method from another future method in Salesforce. This is because both methods are asynchronous and run in separate contexts, and calling one from the other could result in unpredictable behavior. However, you can consider below options:

> Use a Queueable Apex class: Queueable Apex allows you to chain multiple asynchronous jobs together, so you can call a Queueable Apex class from a future method and then call another future method from the Queueable Apex class
    
>Use a Batch Apex class: Batch Apex allows you to process large amounts of data asynchronously, so you can split your processing into multiple batches and call future methods from each batch
    
> Use a scheduled Apex class: Scheduled Apex allows you to schedule Apex code to run at a specific time, so you can schedule a future method to run after another future method has completed

Related: https://www.apexhours.com/future-vs-queueable-apex/

If this information helps, please mark the answer as best. Thank you
    
Rehes RarbaRehes Rarba

Hi, Saravana

If we can't call a future method from another future method, one possible workaround is to use a separate non-future method that can be called from both future methods. This intermediary method can perform the necessary actions or logic required and then be invoked by the initial future methods. This allows for the desired functionality without directly calling one future method from another. This is what I have learned by working at triotech systems and it worked for my clients.