[SalesForce] Getting – First error: Too many future calls: 1

We are trying to track a field change on a record of a managed package object, based on this we are calling out one of our downstream application with rest call.

Step 1- A Managed package batch job is updating a field on Managed package object record.

Step 2 – Taking this a triggering action we are calling one of our downstream application using future methods from trigger.

Step 3 – The managed package job is failing with Too many future calls: 1

We have debugged the execution, observed future call is getting executed only once and the whole code is bulkified, generally when we call asynchronous to future methods below error will come

First error: Future method cannot be called from a future or batch method: WebPortalCLBatchJobStatus.notifyJobStatus(String)     

But couldn't figure out what is this Too many future calls: 1 error

Best Answer

In your code that is calling the future method you will need to wrap it in:

if(!system.isFuture() || !system.isBatch()){
   //trigger your future method
}

If you need to execute your future method as part of this batch process you will have to call the method directly. You can do it by constructing your future as such:

@future
public static myAsyncMethod(Id i){
   myMethod(i);
}

public static myMethod(Id i){
   ....
}

Then in your batch execute myMethod which will do the work skipped by the batch and future check above