About this question
I have a requirement where a future method needs to be called from batch apex.I came across various articles stating that a future method could not be called from the batch. Are there any alternate solutions for that?
I have a requirement where a future method needs to be called from batch apex.I came across various articles stating that a future method could not be called from the batch. Are there any alternate solutions for that?
Log in to share your answer and help other learners.
Log in to answerBest Answer · By JanBask Salesforce Expert
Answered on Mar 2, 2023
The answer to your question - can we call future method from batch class is - The general solution to this and other async problems (i.e. unlimited future calls) was proposed with code samples by Dan Appleman at Dreamforce 13. The code can be found at http://advancedapex.com/dreamforce13/ The essence of the solution is to use a custom object to store all async requests and then have a scheduled class launch a batch job to process each async request one-by-one, restarting the scheduled job in the finish() method if there are new async requests created since the batch was started.
Ranjana Admin JanBask Expert Latest answer
Answered on Jun 4, 2024
No, you cannot directly call a @future method from within a batch class in Salesforce.
@future methods are designed to run asynchronously and are typically used for executing long-running or potentially blocking operations outside the context of the current transaction. They are invoked by the platform's Future Method Executor and execute separately from the main transaction.
Batch classes, on the other hand, are used for processing large volumes of data in chunks, typically within the same transaction context. They are designed to be run synchronously within the context of the current transaction.
However, you can achieve a similar result by implementing the Database.AllowsCallouts interface in your batch class and making a callout to an external service that, in turn, invokes the desired asynchronous process. Here's a basic example:
global class MyBatchClass implements Database.Batchable, Database.AllowsCallouts {
global Database.QueryLocator start(Database.BatchableContext bc) {
// Query the records to be processed
return Database.getQueryLocator([SELECT Id, Name FROM MyObject__c]);
}
global void execute(Database.BatchableContext bc, List scope) {
// Perform any necessary processing
// Make a callout to an external service that invokes the @future method
MyFutureClass.makeAsyncCall();
}
global void finish(Database.BatchableContext bc) {
// Perform any post-processing tasks
}
}In this example, MyFutureClass.makeAsyncCall() represents the callout to the external service, which then invokes the desired @future method. Note that making callouts from batch classes requires careful consideration of Salesforce's governor limits and best practices for handling callouts.
Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.
Step-by-step Salesforce guides from industry experts
Common Salesforce interview questions, answered
Guides, tips and career advice on Salesforce from JanBask experts.
Salesforce How to Become a Salesforce Administrator in 2027: A Step-by-Step Career Roadmap
Want to become a Salesforce Administrator? Get the complete 2026 roadmap skills, certification, salary & career path. Start your…
Salesforce Salesforce Admin Self Study vs Live Training: How to Choose the Right Path
Compare Salesforce Admin self-study and live training by cost, duration, flexibility, mentorship, and career support. Find the…
Salesforce How a Salesforce Admin Certification Shapes Your Career Growth
Wondering if a salesforce admin certification is worth it? See how it shapes hiring, salary, and long term salesforce admin…
Salesforce Salesforce Admin vs Developer Salary: Which Path Actually Pays More?
Salesforce admin vs developer salary: developers earn 15–25% more. See entry, mid and senior pay, why the gap exists, and how…