Activity › Forums › Salesforce® Discussions › Can we call a future method from a Salesforce batch class using Apex?
-
Can we call a future method from a Salesforce batch class using Apex?
Posted by Meddimala Ranjith on April 23, 2018 at 10:58 AMCan we call a future method from a Salesforce batch class using Apex?
kishan replied 5 years ago 7 Members · 7 Replies -
7 Replies
-
Hi ,
we can’t call future method from batch class. 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
- [adinserter block='9']
-
Thank u, Aman, What are the restriction of the future method?
-
Hi,
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.
Yes, we can’t call future method from batch class.
Some restriction of the future method are:
- No more than 50 method {Methods with the future annotation} calls per Apex invocation
- The maximum number of future method invocations per a 24-hour period is 250,000 or the number of user licenses in your organization multiplied by 200, whichever is greater. This limit is for your entire org and is shared with all asynchronous Apex: Batch Apex, Queueable Apex, scheduled Apex, and future methods.
- Methods with the future annotation must be static methods
- Can only return a void type
- You can invoke future methods the same way you invoke any other method. However, a future method can’t invoke another future method
- Methods with the future annotation cannot be used in Visualforce controllers in either getMethodName or setMethodName methods, nor in the constructor.
- You cannot call a method annotated with 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.
- The getContent and getContentAsPDFPageReference methods cannot be used in methods with the future annotation
For access to higher limits for future methods, and to invoke a future method from another future method, use the Future Methods with Higher Limits pilot.
Hope this helps you more.
-
Hi,
NO we can’t call async method from another async method, it will throw System.Async Exception.
Hope this helps.
-
Hi Aman,
is there any work around to call future from batch class? -
No, We cannot call future methods directly from batch apex but we can call a web service from batch class and that web service can call the @future method. Also, we can call the future method from finish method in the batch class.
Log In to reply.