0

I am receiving the following error for some of my batches every here and there:

You've exceeded the limit of 100 jobs in the flex queue for org 00D6A000000vKLe. Wait for some of your batch jobs to finish before adding more.

Well, how can I have my code "Wait" until there is more space in the queue?

1

You probably need to check the number of jobs and then use an integer to make sure that there is a space for a new job. You can use this query to get exactly that:

Integer flexQueueSize = [
   SELECT COUNT() 
   FROM AsyncApexJob 
   WHERE Status = 'Holding' FOR UPDATE];

I think that this should help you to avoid reaching the flex queue limit.

A more sophisticated solution can be found here: https://www.jitendrazaa.com/blog/salesforce/framework-to-fix-governor-limit-of-100-jobs-in-flex-queue/

  • The framework approach is good if you control the majority of the code using the batch queues. Not so great if you don't. It is quite strange that the flex queue is as limited as it is, TBH. I guess it is because it allows for a very large JSON serialization footprint for the Batchable instance being executed/queued. – Phil W Oct 21 '19 at 21:10
  • What does FOR UPDATE do? Why is it helpful in this scenario - Just trying understand the foundation of this query. – Matthew Metros Oct 23 '19 at 12:27
  • FOR UPDATE basically locks the jobs and it will not allow any changes. The status Holding is related to Flex Queue jobs, up to 100.. help.salesforce.com/… Use the Integer to find out how many are already in Holding. – Carlos Naranjo Oct 23 '19 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.