Sub topic 1
Where Scheduled Apex fits among asynchronous options
Prerequisites
This chapter assumes you can already do the following:
- Read and write basic Apex classes and methods.
- Write SOQL queries and run DML statements.
- Explain at a high level what governor limits are and why Salesforce enforces them.
- Open Setup and the Developer Console.
You don't need to have written any asynchronous Apex before.
What asynchronous Apex means
Most Apex you write runs synchronously. A user saves a record, a trigger fires, and the user waits until the code finishes. Asynchronous Apex runs later, in its own transaction, after the request that asked for it has finished. Each async job gets a fresh set of governor limits, and many of those limits are higher than the synchronous ones.
Salesforce offers four main ways to run Apex asynchronously:
- Future methods are static methods marked
@future. They suit short, fire-and-forget work, such as a callout that must not block a trigger. - Queueable Apex is a class that implements
Queueable. You start it withSystem.enqueueJob. It accepts complex parameters such as sObjects and can start another job when it finishes. - Batch Apex is a class that implements
Database.Batchable. It splits a large record set into chunks. Each chunk runs in its own transaction with its own limits. - Scheduled Apex is a class that implements
Schedulable. It runs at times you define with a cron expression, with no user or record event starting it.
The first three answer the question "do this work, but not right now." Scheduled Apex answers a different question: "do this work at a particular time, and keep doing it."
What makes Scheduled Apex different
The other three options are started by code that is already running. A trigger enqueues a job, or a controller calls a future method. Scheduled Apex is started by the platform clock. You register a job once, and Salesforce runs your class on the timetable until you abort the job.
Scheduled Apex also has a smaller role than the others. Its execute method runs in an asynchronous transaction, so it has the same asynchronous limits. It is a good place to start work and a poor place to do heavy work. Chapter 7 covers handing large workloads to Batch Apex in detail.
| Option | Started by | Best for | Runs repeatedly on its own |
|---|---|---|---|
| Future method | Your code | Short isolated work, such as a callout | No |
| Queueable Apex | Your code | Work with object parameters or chained steps | No |
| Batch Apex | Your code or a scheduled job | Processing very large record sets | No |
| Scheduled Apex | The platform clock | Recurring or time-specific work | Yes |
Typical uses
Choose Scheduled Apex when the trigger for the work is time, not an event. Common cases are:
- A nightly job that closes or archives old records.
- A weekly summary that is calculated and emailed on Monday morning.
- A job that runs once at a known future time, such as a campaign end date.
- A periodic sync that pulls data from an external system.
If the trigger for the work is a record change, use a trigger, a flow, or an event-driven async option instead. Polling for changes with a scheduled job is slower and uses more of your limits.
Choosing between the options
The diagram shows one way to decide.
The options combine well. A scheduled class often does nothing except start a batch job, so the timetable lives in one class and the heavy processing lives in another.
Common mistakes
- Using a scheduled job to poll for events. Run the logic from a trigger, flow, or platform event, which fires when the change happens.
- Expecting exact start times. A scheduled job starts when the time arrives and system resources are available. It can start slightly late, so don't build logic that depends on the exact second.
- Putting large processing in
execute. The method gets asynchronous limits, which are higher than synchronous limits but still finite. Hand large volumes to Batch Apex. - Forgetting that jobs persist. A scheduled job keeps running until it is aborted or its timetable ends. Test jobs and old jobs left in an org keep firing.
What comes next
The next chapter shows how to write the class every scheduled job needs, a class that implements Schedulable. After that you will write cron expressions, register jobs from code and from Setup, and learn to monitor, abort, and test them.
Exercise
A team needs to recalculate a summary field on every Account at 2:00 AM each night, and the org has several hundred thousand accounts. Which asynchronous options would you combine, and why?
Show answer
Use Scheduled Apex to start the job at 2:00 AM each night, because the trigger is time and the work repeats. Have its execute method start a Batch Apex job, because the data volume is too large for one transaction. Batch Apex splits the accounts into chunks, and each chunk gets its own governor limits.