2

I was testing Finalizer interface with Queueable apex and I think it can also be used to overcome this error: System.LimitException: Too many queueable jobs added to the queue: 2

I created a class QueueTest

public with sharing class QueueTest implements Queueable, Finalizer{

    private List<List<String>> strings;
    public QueueTest(List<List<String>> strings){
        this.strings = strings;
    }
    public void execute(FinalizerContext param1) {
        System.enqueueJob(new QueueTest1());
    }

    public void execute(QueueableContext param1) {
        QueueTest queueTest = new QueueTest(strings);
        System.attachFinalizer(queueTest);
        System.enqueueJob(new QueueTest1());
        System.debug('Queueable executed');

    }
}

and here I am firing two Queueable calls from same QueueTest class. And I dont see any error.

But when I try to fire same back to back queueable from same execute method I get the error Too many queueable jobs added to the queue: 2.

Theoretically both are same, right? Or was finalizer created to overcome such limitations?

1 Answer 1

4

Finalizer was meant to handle LimitException situations in general (too much CPU, heap, etc), as well as any uncaught exceptions, so you can recover from the situation. It is not meant to be a workaround for the too many jobs situation specifically. Note that the Finalizer itself is limited to only one additional action (a Batchable, Queueable, or @future).

Inside the execute(QueueableContext) method, execute(BatchableContext), and @future methods, you are limited to just one Queueable job per execute. If you need to do more than one Queueable in an execute, use a Scheduled job if you can, as those have normal synchronous limits (e.g. 50 Queueable/@future, multiple batches, etc).

3
  • so if i am using queueables to update readonly records in the after trigger and more then 201 records are being updated, thereby causing 2 jobs to enqueue, i can over come this simply by schedualing my queueables instead?
    – Dane
    Commented Nov 21, 2022 at 14:34
  • @Dane You can't schedule queueable. Once you've gone to running DML in asynchronous code, any further asynchronous code must be coordinated to avoid the limit of just one async transaction. I address this concern on this answer that may be of use to you.
    – sfdcfox
    Commented Nov 21, 2022 at 14:48
  • Thank you so much. Your contributions are amazing.
    – Dane
    Commented Nov 22, 2022 at 10:47

You must log in to answer this question.

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