function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MJ09MJ09 

System.AsyncException: Failed to enqueue future method

I have an after insert trigger on a custom object. The trigger calls a future method (a method defined with @future), passing in Trigger.newMap.keySet().

 

When I run unit tests that insert records into the custom object, the trigger does its job just fine. The unit tests confirm that the future method does its work and creates some records for a different object.

 

However, when I use the Execute Anonymous window in the IDE to run the same code (I literally copied the code from the unit test and pasted it into the Execute Anonymous pane), the Apex code completes without error, but then I get an email message that says "System.AsyncException: Failed to enqueue future method." The same thing happens when I cause the trigger to fire by inserting records into the custom object through a custom JavaScript button on a standard page in the browser.

 

I commented out the line in the trigger that calls the future method (I'm running in a development org), inserted the records into the custom object, then ran some anonymous Apex code that builds a Set of the new record IDs and calls the future method. That runs just fine -- no email message, and the future method does its work.

 

The code is pretty simple. The trigger simple does something like:

 

myClass.myFutureMethod(Trigger.newMap.keySet());

 

And the method is simply defined:

 

global class myClass { @future public static void myFutureMethods(Set<Id> setIds) { .....

 

For what reasons might my future method not get enqueued? Why does it get enqueued and run just fine in a unit test, but not otherwise?

 

Thanks for any insight you can provide.

MJ09MJ09

Just wanted to document the answer to this. I submitted a case on this problem, and got a very prompt response from Support saying that there is a known problem with passing a Trigger's map's keyset to an async method. The workaround is to copy the Trigger's keyset to a new set and then call the method.For example:

 

change :
    myClass.myFutureMethod(Trigger.newMap.keySet());

to:
    Set<Id> ids = new Set<Id>(Trigger.newMap.keyset());

    myClass.myFutureMethod(ids);

 

It doesn't seem like this should make a difference, but it does, and it solved the problem!

 

 

JoeK.ax278JoeK.ax278

MJ,

 

Thank you so much for posting your resolution to the issue as you saved me a good hunk of time!   Apparently, this is still an issue in a slightly different guise.   I hit the problem when trying to pass the keySet() from a map returned by SOQL expression:

 

 

 

Map<Id,Lead> requestLeads = new Map<Id,Lead>( [select id, ... from Lead where ... limit 10]); ... invokeSomeFutureMethod(requestLeads.keySet());

 

 

The fix you prescribed worked here as well.

 

Think & Enjoy -- JoeK

 

 

 

 

JoeK.ax278JoeK.ax278

 

I susequently hit a similar problem with the result of keySet():  removing elements from the keySet() it answers removed them from the dictionary as well!    I now routinely clone keySet() responses.

 

T&E -- JoeK