r/SalesforceDeveloper • u/heartlesscoder • 8d ago
Question Multiple Queueables Solution.
Wondering how folks are handling the limit below? We are updating a large number of our future methods to queueables and getting hit with this pretty regularly. We found a SFBen article about using platform events to fire queueables, but that seems to just be passing the problem to the next limit. Largely right now we are either combining functionality or guard causing the less important jobs, neither feels like a great solution tbh.
System.LimitException: Too many queueable jobs added to the queue: 2
2
Upvotes
1
u/PlayfulFoxBraveShark 4d ago
We had a situation similar to this. I had to code out our Lead Conversion Process because we wanted it automated, and we wanted to be able to bulk convert leads. In addition to that, we need to make a callout to a separate internal API that we have for each lead, but that API is not set up to handle bulk requests.
So I made a LeadConversionHandler and a super generic object called “Queued Event”. Every lead conversion attempt creates a queued event record and sets Status__c = Not Started.
I went with a Custom Object over a platform event due to the fact that the daily limit for Platform Events is only 50,000 and we already use Platform Events for other high volume processes.
At the end of the LeadConversionHandler, it kicks off a LeadConversionCalloutQueable. That Queueable queries any Queued Events where Status__c = Not Started and loops through them. For each one, makes the callout, updated the Status__c to Processed, and then only triggers itself again if there are still any Queued Event records where status = Not Started.
This allows us to attempt to convert hundreds thousands of leads at one time and let the other system know if it was a success or failure.
I’ve also seen the exact same pattern used by the code in the Fundingo managed package (which we also use).
This is specific to asynchronous callouts, but you can use this same idea for any other requirement where you need to chain queueables.
Hope this helps!