The error "Too many queueable jobs added to the queue" occurs when you attempt to queue more than the allowed number of queueable jobs in a single transaction.
In
a queueable apex, You can add up to 50 jobs to the queue with System.enqueueJob in a single transaction.
Let us understand this with an example:
Below is sample queueable class.
public class SampleQueueableJob implements Queueable {
public void execute(QueueableContext context) {
system.debug('Queueable job called');
}
}
We are going to call the above queueable class from below class which is enqueuing the queueable class 50 times.
public class LaunchQueueableJobClass{
public static void launchQueueableJobs() {
// Chain multiple instances of the Queueable job
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
}
}
Let's call the LaunchQueueableJobClass apex class from developer console as shown below.
Now, check the status in Apex Jobs under setup. You will be able to see the queueable apex is executed 50 times.
Now, let us try to add one more System.enqueueJob(new SampleQueueableJob()); statement in LaunchQueueableJobClass apex class. If you try to run the code from developer console now you will be able to see the below error.
public class LaunchQueueableJobClass{
public static void launchQueueableJobs() {
// Chain multiple instances of the Queueable job
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob());
System.enqueueJob(new SampleQueueableJob()); // Added 51 time...
}
}
No comments:
Post a Comment