Batch Apex in AppExchange package and orgs with related triggers that call @Future methods

Here’s the problem: when an org has a trigger that calls an @Future method, any batch Apex that fires that trigger will fail, since you cannot call an @Future method from an @Future method OR from batch Apex. If you do, the following error results:

Type: System.DmlException Message: Update failed. First exception on
row 0 with id 00Q3000000y3sIZEAY; first error:
CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, methodNameHere: execution of
AfterUpdate caused by: System.AsyncException: Future method cannot be
called from a future or batch method

So, if you have a managed package on the AppExchange that includes batch Apex that, for example, updates contacts periodically, and this package is installed into an org that has @Future calls made from inside a contact trigger, your batch Apex will fail to execute.

Has anyone worked through this issue or perhaps does anyone have any suggestions around what options might be available for this scenario?

Thanks in advance!

Answer

Since it turns out System.AsyncException is uncatchable (just tested it) there isn’t much you can do to either predict when this is going to happen or resolve it outside of editing the conflicting code.

In short: there’s not really a solution other than requiring customers to fix their code to not use @future form non-async safe contexts. And yes, I realize how well that response goes over, I’ve had to give it more than once.

Attribution
Source : Link , Question Author : Adam , Answer Author : ca_peterson

Leave a Comment