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
Alper OzdamarAlper Ozdamar 

How can we call a future method from a batch class?

I need to send Asynchronous requests to some 3rd party API and that call method should be @future. 
On the other hand, we also want to call this method from Batch class. But Salesforce doesn't allow us to send future method from a batch class?

Any ideas?
 
Best Answer chosen by Alper Ozdamar
Bryan Leaman 6Bryan Leaman 6
What I do is code something like this:

public String DirectCalloutMethod(...) {
    // perform the callout
}

@future
public String FutureCalloutMethod() {
    return DirectCalloutMethod();
}


In the batch process use "DirectCalloutMethod" and everywhere else use "FutureCalloutMethod".

All Answers

ShirishaShirisha (Salesforce Developers) 
Hi Alper,

Greetings!

Unfortunately,you can't call the future method from a batch call as it will throw the error as below:

Future method cant be called from future or Batch

Kindly let me know if it helps you and close your query by marking it as best answer so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
Alper OzdamarAlper Ozdamar
Thanks, Shirisha. Are there any workaround solution?
Bryan Leaman 6Bryan Leaman 6
What I do is code something like this:

public String DirectCalloutMethod(...) {
    // perform the callout
}

@future
public String FutureCalloutMethod() {
    return DirectCalloutMethod();
}


In the batch process use "DirectCalloutMethod" and everywhere else use "FutureCalloutMethod".
This was selected as the best answer
Alper OzdamarAlper Ozdamar
Thank you Bryan. This helps me a lot.