Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

How to resolve this error - too many queueable jobs added to the queue 2?

Asked by Claudine Tippins Apr 17, 2023 8.7K views 3 answers
Share

About this question

I've been looking for similar questions, but none of them applies to my scenario, so I'd be grateful if you can explain to me why my logic is failing, and what I can do in order to fix it.

I want to run every month a logic on Account records (>50k) : BATCHS

This logic will create X events on each account : Event Trigger For new Events, I need to sync the data with other system : @future method So, since Batchs cannot call other future methods, I decided to implement the @future logic inside a Queueable class.

Here is a snippet:

Batch
global class BATCH_GeneradorEventos implements Database.Batchable
{
       ...
       global void execute(Database.BatchableContext BC, List scope)
       {
       ...
          insert evs;
       }
       ...
}
Event trigger
ID jobID = System.enqueueJob(new EventQueueableJob(Trigger.newMap.keySet()));
Queuable class
public class EventQueueableJob implements Queueable, Database.AllowsCallouts
{
    ...
    public void execute(QueueableContext context) 
    {
      // future methods and logic
    }
}
The error that I'm getting is:
"Too many queueable jobs added to the queue: 2"

and it's giving me for every SerialBatch.

Is it because I'm doing an enqueueJob for every iteration of the Batch? If so, how can I fix/remodel it?

Your answer

3 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on Jan 28, 2025

If you’re encountering the error "Too many queueable jobs added to the queue: 2" in Salesforce, it’s because Salesforce allows only one Queueable job to enqueue another Queueable job in a single transaction. If more than one is attempted, this error occurs. Here’s how you can resolve it:


1. Understand the Limit

  • Salesforce enforces a limit of one Queueable job chaining per transaction.
  • When a Queueable job is already in progress and tries to enqueue another, the system throws this error if it exceeds the limit.

2. Chain Queueable Jobs Properly

  • If you need to chain multiple Queueable jobs, ensure that only one is enqueued at a time.
  • Example of proper chaining:

public class FirstQueueable implements Queueable {
    public void execute(QueueableContext context) {
        // Perform task
        System.enqueueJob(new SecondQueueable());
    }
}

  • This ensures only one job is added per transaction.

3. Use Future Methods or Batch Apex for Parallel Processing

  • If you need to process multiple tasks simultaneously, consider using @future methods or Batch Apex instead of Queueable jobs, as they don’t have this limitation.

@future
public static void processTask() {
    // Perform task
}

4. Aggregate Logic in a Single Job

  • Combine logic from multiple Queueable jobs into one job to reduce the need for multiple enqueues.
  • This reduces the complexity and avoids hitting the limit.

5. Offload Work to Scheduled Jobs

  • For tasks that don’t need immediate execution, use Scheduled Apex to process them in batches without relying on Queueable chaining.

6. Check Debug Logs

  • Use logs to identify where multiple jobs are being enqueued and optimize the logic to avoid exceeding the limit.

By following these strategies, you can ensure proper handling of Queueable jobs while staying within Salesforce’s limits. Let me know if you need help implementing any of these!

Was this helpful?

Ranjana Admin JanBask Expert

Answered on May 2, 2024

The error "too many queueable jobs added to the queue" typically occurs in systems that utilize a queuing mechanism for background or asynchronous processing, such as in Salesforce with its Queueable Apex jobs. This error indicates that you are adding more Queueable Apex jobs to the queue than the system's governor limits allow.


Here are some steps you can take to resolve this error:

Reduce the Number of Queueable Jobs: Review your code to see if you can optimize it to reduce the number of Queueable Apex jobs being added to the queue. Are there any unnecessary or redundant jobs being queued? Can you combine multiple jobs into one to reduce the overall load on the queue?

Check for Recursive Queueable Calls: Make sure that your Queueable Apex jobs are not calling themselves recursively, as this can quickly escalate the number of jobs in the queue and lead to this error.

Implement Throttling Mechanisms: Implement mechanisms in your code to throttle the number of Queueable Apex jobs being added to the queue within a certain time period. You can use custom logic to control when and how many jobs are queued based on factors such as system load or available resources.

Optimize Job Execution: Review the execution time of your Queueable Apex jobs and optimize them for efficiency. This can help reduce the likelihood of hitting governor limits related to asynchronous processing.

Monitor and Tune: Continuously monitor the performance of your system and tune your code as necessary to prevent hitting governor limits. Keep an eye on the number of jobs in the queue and adjust your code accordingly.

Consider Alternative Solutions: Depending on your specific use case, consider alternative solutions such as Batch Apex or Scheduled Apex if they better suit your requirements and help avoid hitting governor limits.

By following these steps and carefully managing the number of Queueable Apex jobs being added to the queue, you should be able to resolve the "too many queueable jobs added to the queue" error in your Salesforce org.

Was this helpful?

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.