+ Start a Discussion
kgrasukgrasu 

System.AsyncException:Future method cannot be called from a future or batch method

Hi,

Is System.AsyncException (Future method cannot be called from a future or batch method) apply to appexchnage partner program?

We have installed an AppExchange which is a managed package  and also we have our own code (batch apex). Whenever we run the batch job; the future callout in the managed package is error out a System.AsyncException.
The managed package code snippet are:

Trigger:
trigger <triggername> on opportunity (before update){
//checking condition
If(Check__c == true);
//collect the info and call future method
Classname.updatemethod(data);


Class:
Public with sharing class classname{
@future (callout=true)
    public static void updatemethod(Sring data) {
//send data to external system
}

We running a batch job daily on opportunity and updating the check__c field based on some condition.

So basically, if the batch job update the check__c = true then the trigger in managed package fire and collect the info and send to the external system by future callout.

Please refer the error message below:
Update failed. First exception on row 0 with id 0066000000Qzl22AAB; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, triggername: execution of BeforeUpdate caused by: System.AsyncException: Future method cannot be called from a future or batch method: Classname.updatemethod(data);

How do we resolve this kind of issues between client’s batch job and appExchange’s future callout during an update operation.

Thanks and regards
Govinda
ShashankShashank (Salesforce Developers) 
Hi Govinda,

The only way is to avoid the trigger from firing if it is invoked by Batch Apex. YOu can use the suggestion in this page: http://salesforce.stackexchange.com/questions/47261/future-method-cannot-be-called-from-a-future-or-batch-method-case-trigger-with

Thanks,
Shashank