0

How can I solve this problem? Can you please give me an example or an idea for a potential solution?

Boris Bachovski
  • 16,216
  • 8
  • 47
  • 85
salesforce
  • 99
  • 2
  • 9
  • 16

2 Answers2

0

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

Amit Bangad
  • 3,890
  • 1
  • 15
  • 19
0

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.

A.K.
  • 708
  • 1
  • 8
  • 19