How can I solve this problem? Can you please give me an example or an idea for a potential solution?
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
-
How do you find more official resources like that pdf? Commented Sep 19, 2016 at 17:40
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.