How can I solve this problem? Can you please give me an example or an idea for a potential solution?
-
This SFSE thread may help you. Please check. – Saroj Bera Jul 22 '15 at 06:06
-
Or this thread : http://salesforce.stackexchange.com/questions/24843/calling-future-method-from-batch/24853#24853 – Eric Jul 22 '15 at 13:57
2 Answers
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
But noteworthy point here is each execute method of batch apex is executed in asynchronously using its own resources and governor limits as an @future methods. So I would suggest you to give a try by putting logic in execute() method
Reference - https://resources.docs.salesforce.com/194/latest/en-us/sfdc/pdf/salesforce_async_processing.pdf
- 3,890
- 1
- 15
- 19
You can`t call future methods from Batch context.
I believe that workarounds for this depends on why you are using future methods in your code.
If you need to do a callout then you can remove @future annotation and do it directly from your Batch but you might want to reduce Batch size because of limits. Also reducing Batch size might help if you are using @future to have more resources for some methods.
If your Batch is triggering some code that does future calls you might want to add conditions to that code like
if(System.isFuture() || System.isBatch()){
//Your logic here
} else {
//Mark records with some NeedToProcess__c = true; and then process them in another Batch
//moving future logic to that Batch. Or just move your future logic to your Batch and do
//nothing in else case.
}
This thread might help you as you can schedule some Schedulable class from your Batch which can invoke future methods.
- 708
- 1
- 8
- 19
- 31
- 3