System.LimitException: Too many queueable jobs added to the queue

System.LimitException: Too many queueable jobs added to the queue

1. You can add up to 50 jobs to the queue with System.enqueueJob in a single transaction.

2. When chaining jobs with System.enqueueJob, you can add only one job from an executing job. Only one child job can exist for each parent queueable job. Starting multiple child jobs from the same queueable job isn’t supported.

3. System.LimitException: Too many queueable jobs added to the queue: 2

You can only create one queueable job within a batch execution.

Sample code to reproduce:

Queueable Interface:

public class SampleQueueable implements Queueable {
    public void execute(QueueableContext context) {
    
        system.debug( 'Inside Queueable Interface' );
        
    }
    
}

Batch Apex:

global class SampleBatch implements Database.Batchable<sObject> {
    
    global Database.QueryLocator start( Database.BatchableContext BC ) {
    
        String strSOQL = 'SELECT Id FROM Account LIMIT 2';
        return Database.getQueryLocator( strSOQL );
        
    }
    
    global void execute( Database.BatchableContext BC, List< Account > listAccounts ) {
    
        for ( Account acc : listAccounts ) {
            System.enqueueJob( new SampleQueueable() );//Will throw an exception
        }
        
    }   
    
    global void finish( Database.BatchableContext BC ) {
    }
    
}

Code to execute to reproduce the issue:

Database.executeBatch( new SampleBatch() );

Limits.getQueueableJobs(): 

getQueueableJobs() Returns the number of queueable jobs that have been added to the queue per transaction. A queueable job corresponds to a class that implements the Queueable interface.

Leave a Reply