Concept 8 of 10Asynchronous Apex

Scheduled Apex

Running jobs on a timetable with Schedulable and cron expressions, the scheduled-job limit, and why the scheduled class should only start other work.

Your confidence:Not rated

Simple explanation

A class implementing Schedulable has one method, execute(SchedulableContext sc). Schedule it from Setup (Apex Classes β†’ Schedule Apex) or in code:

// Seconds Minutes Hours Day-of-month Month Day-of-week [Year]
String nightly = '0 0 2 * * ?';          // 02:00 every day
System.schedule('Account health nightly', nightly, new AccountHealthScheduler());

Good practice

  • Keep the scheduled class thin: it should start a batch or queueable, not do the work itself.
  • System.scheduleBatch(batch, jobName, minutesFromNow, scope) runs a batch once after a delay.
  • Use named jobs and keep a runbook of schedules, owners and expected runtimes.

Limits: at most 100 scheduled Apex jobs at a time per org. A class that is scheduled can't be edited or deployed while it's scheduled, unless the org allows deployments with active jobs βš‘ verify the current setting name.

Real-world analogy

A scheduled class is an alarm clock. Its only job is to wake the right person (a batch or queueable) at the right time. If the alarm clock tries to cook breakfast itself, mornings go wrong.

Worked example

public with sharing class AccountHealthScheduler implements Schedulable {
    public void execute(SchedulableContext sc) {
        Database.executeBatch(new AccountHealthBatch(), 500);
    }
}

Diagram

Rendering diagram…

Key points

  • Schedulable.execute + cron expression; schedule in Setup or with System.schedule.
  • Keep it thin: start a batch or queueable.
  • System.scheduleBatch for a one-off delayed batch.
  • Up to 100 scheduled jobs per org.
  • Scheduled classes block deployments of the same class unless the org setting allows it.

Limits and gotchas

  • Schedules run in the time zone of the user who scheduled them. Document it, especially across daylight-saving changes.
  • A scheduled job is tied to the user who scheduled it; if that user is deactivated, the job fails. Use an integration user.
  • Scheduled jobs aren't guaranteed to start at the exact second; they start when resources allow.
  • Avoid scheduling a job every few minutes to poll; consider platform events or change data capture instead.

🎀 How to say this in an interview

A short concept, but interviewers like the deployment gotcha (scheduled classes blocking deployment) and the integration-user ownership point. Show you separate scheduling from processing.

Common follow-ups

How do you schedule a job every 15 minutes?

A single cron expression can't express 'every 15 minutes' in Apex scheduling, so teams schedule four jobs at minutes 0, 15, 30 and 45, or have a job reschedule itself. First question whether polling is needed: events or change data capture are often better.

A deployment fails because a class is scheduled. What do you do?

Either enable the deployment setting that allows deploying with active scheduled jobs (if your release process accepts the risk), or unschedule the job before deployment and reschedule it afterwards in a scripted post-deployment step.