function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jonathan SpinkJonathan Spink 

How do I ensure serial processing of record merges?

In an Apex test I am merging three pairs of Contacts (in a for loop), but before it completes I get this error:

first error: LIMIT_EXCEEDED, System.LimitException: npsp:Too many queueable jobs added to the queue: 2: []

How can I ensure only 1 queueable job is processed at one time?
SwethaSwetha (Salesforce Developers) 
HI Jon,
I came across these related posts that can help
https://salesforce.stackexchange.com/questions/207441/error-too-many-queueable-jobs-added-to-the-queue-2

https://salesforce.stackexchange.com/questions/187069/system-limitexception-too-many-queueable-jobs-added-to-the-queue

https://salesforce.stackexchange.com/questions/196827/using-batch-and-queueable-causing-too-many-queueable-jobs-added-to-the-queue-2

Thank you
IqbalIqbal
Hi Jonathan,

As per salesforce documentation, you can't add more than 1 job in the queue. It is a limitation for test class.

You can’t chain queueable jobs in an Apex test. Doing so results in an error. To avoid getting an error, you can check if Apex is running in test context by calling Test.isRunningTest() before chaining jobs.

you can try to add next job in the queue when first is completed. Use the below query and only add if status is completed/finish check as per docs. Maybe you can also try to check if it is running from test class add test class check and dont add job in the queue. 
AsyncApexJob jobInfo = [SELECT Status,NumberOfErrors FROM AsyncApexJob WHERE Id=:jobID];

reference: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_queueing_jobs.htm

Please mark this answer if it helps you and close this request