0

I am confused by the asynchronous Apex limits. The limit for number of batch jobs in the flex queue is 100, but the max number for batch jobs queued or active concurrently is 5. So my question is would the following code hit the second limit or not?

Database.executeBatch(myClass1, 200);
Database.executeBatch(myClass2, 200);
Database.executeBatch(myClass3, 200);
Database.executeBatch(myClass4, 200);
Database.executeBatch(myClass5, 200);
Database.executeBatch(myClass6, 200);

If it does hit the limit, what is the recommended workaround?

4

As long as there are enough slots left in the flex queue, you won't get an exception. If you exceed the 105 limit (5 active plus 100 queued), then you'll get the exception you're expecting. If you need to check, you can always try-catch an AsyncException:

try {
    // do execute batch here
} catch(AsyncException e) {
    // Decide what to do here, report error, etc.
}
| improve this answer | |
  • What's the difference between queued and active? How can I stop them activating if there are more than 5 active? – fred Oct 30 '18 at 13:32
  • @fred Active means it has started running the start/execute/finish methods, queued means it is waiting for the five active ones to finish before starting another (the flex queue). Batchable jobs will not become active while five jobs are already active. – sfdcfox Oct 30 '18 at 13:35
  • Won't the exception be a LimitsException and not an AsyncException? That would mean you can't catch it. – fred Oct 30 '18 at 13:49
  • 1
    @fred No, even though it's a sort of governor limit, this exception isn't as fatal, and Salesforce will allow you to handle the situation gracefully with a try-catch. – sfdcfox Oct 30 '18 at 13:53

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.