Sub topic 10
Limits and restrictions to plan around
This chapter assumes you can write a Schedulable class, read a cron expression, and call System.schedule. It also assumes you know the main governor limits. Here you learn which limits and restrictions apply to scheduled jobs, so you can design around them before they cause failures in production.
How much scheduling you can do
Three limits cap how many jobs you can run.
| Limit | Value | What it means |
|---|---|---|
| Scheduled Apex jobs at one time | 100 | A new `System.schedule` call fails once 100 jobs are waiting to run. |
| Asynchronous Apex executions per 24 hours | 250,000, or licenses × 200 if higher | Scheduled, batch, queueable, and future work all share this pool. |
| Batch jobs queued or active | 5 | Extra batch jobs wait in the flex queue with status Holding. |
Each scheduled job holds one of the 100 slots until it has run for the last time or you abort it. A job with a repeating cron expression holds its slot indefinitely. The following query shows how many slots you are using. Job type '7' is Scheduled Apex.
Integer used = [
SELECT COUNT()
FROM CronTrigger
WHERE CronJobDetail.JobType = '7'
];
System.debug(used + ' of 100 scheduled Apex slots in use');If you need a job to run more often than your cron granularity comfortably allows, you might create several jobs with offset start times. This is costly, because every copy uses one of the 100 slots. A single job that finds the work and hands it to Batch or Queueable Apex is usually cheaper.
Governor limits inside execute
The execute method runs asynchronously, so it gets the asynchronous governor limits. These include 200 SOQL queries, 12 MB of heap, and 60 seconds of CPU time per transaction. They are larger than the synchronous limits, but they still apply to the whole execute call. Large data volumes still need Batch Apex, which gives every batch a fresh set of limits.
Restrictions on what the job can do
Callouts
You can't make a synchronous callout from execute. There are two supported ways around this:
- Call a method annotated with
@future(callout=true)fromexecute. - Start a batch class that implements
Database.AllowsCallouts.
This example uses the first approach. It assumes a Named Credential called Inventory_API exists in your org.
public class StockSyncService {
@future(callout=true)
public static void pull() {
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:Inventory_API/stock');
req.setMethod('GET');
HttpResponse res = new Http().send(req);
System.debug('Status: ' + res.getStatusCode());
}
}
public class StockSyncJob implements Schedulable {
public void execute(SchedulableContext sc) {
StockSyncService.pull();
}
}Other restrictions
PageReference.getContent()andgetContentAsPDF()are not supported in scheduled Apex.- There is no user session. The job runs in system mode, so don't rely on
UserInfovalues that assume an interactive user. - A scheduled job can start a batch job or queue a queueable job. Keep in mind that each one counts against its own limits.
Timing is not exact
Salesforce queues the job at the scheduled time. It starts when resources are available, so the actual start can be later. Don't write logic that assumes the job runs at an exact second. Compute time windows from stored data, not from System.now() at the moment execute starts.
Changing a class that is scheduled
By default, Salesforce blocks you from saving or deploying a change to an Apex class while a scheduled job references it. You will see an error saying the class has pending or in-progress jobs. You have two options:
- Abort the job, deploy, and schedule it again.
- In Setup, open Deployment Settings and allow deployments when Apex jobs are pending or in progress.
Option 1 is safer for a one-off change. Option 2 suits teams that deploy often, but you should check that a job isn't mid-run during the deployment.
Where the limits apply
Common mistakes
- Scheduling a job inside a loop or trigger without checking how many jobs already exist.
- Forgetting that the 250,000 daily limit is shared, so a busy batch process can starve your other async work.
- Making a plain callout in
executeand finding the failure only after the first run. - Leaving test or abandoned jobs scheduled so they quietly fill the 100 slots.
Exercise
Your scheduled job's execute method loads 80,000 records and then calls an external service for each one. Name two limits or restrictions it will hit and say how you would redesign it.
Show answer
The loop of callouts breaks the rule against synchronous callouts in scheduled Apex. Loading 80,000 records in one transaction also risks the heap, CPU, and query row limits. Redesign it so execute only calls Database.executeBatch with a batch class that implements Database.AllowsCallouts. Choose a scope size small enough for the callout limits, and let each batch run with its own governor limits.