5

I need to do five different callouts from my trigger. I'm planning to use one Queueable class for all the callout using the parameter passed in the constructor. I'm not sure if it is possible, if it is then can I do parallel enqueue like:

System.enqueueJob(new QueueableClass(newList[0]));
System.enqueueJob(new QueueableClass(newList[0]));
System.enqueueJob(new QueueableClass(newList[0]));
System.enqueueJob(new QueueableClass(newList[0]));

or do I need to chain it. if its not possible then, I guess I need to create new class for each callout.

1
  • 1
    of course, one queueable can do multiple callouts (up to the limit per Txn and up to the limit of callout times per Txn) so this pattern may not be necessary
    – cropredy
    Commented May 15, 2019 at 18:10

1 Answer 1

11

This pattern is likely to cause tricky-to-debug issues in the future.

When your trigger is invoked via updates made in a Batch Apex class, you will get an exception stating that you've enqueued too many jobs at 2.

Too many queueable jobs added to the queue: 2

As you may know, the limit per transaction is 50 (so if you're enqueuing five Queueables per trigger record - I see newlist[0] in there - you're already in trouble with regular trigger operation). However, an Apex Batch context only allows enqueuing one job, not 50.

As a result, down the road when you write a Batch that directly or indirectly affects this object, you'll start to get these mysterious failures when the trigger runs and tries to enqueue so many Queueables.

For more, see this question and answer.

You should use a chaining pattern here. If your callouts are not idempotent, you also need to be very careful about the possibility of trigger recursion.

2
  • Even if the callouts are idemptotent, a recursive trigger could blow through a very large portion of the daily limit of callouts.
    – Adrian Larson
    Commented May 15, 2019 at 21:33
  • 1
    For this scenario, I am always a fan of writing the records/record ids to a queue transaction table, and triggering the process there - that way you can trap errors and build the ability to retry etc Commented May 16, 2019 at 0:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .