Sub topic 5
Scheduling from Setup
This chapter assumes you can write a Schedulable class, read a basic cron expression, and find your way around Setup. If not, review the earlier chapters on the Schedulable interface and cron syntax first.
Why use the Setup UI
System.schedule gives you full control, but it needs code. Setup offers a form for the same task. It suits admins and one-off jobs, and it lets you schedule a class without opening the Developer Console. The trade-off is a narrower choice of timetables.
A class ready for the UI
Setup creates the job by instantiating your class itself. It calls a constructor with no arguments, so it cannot supply values. A class that needs constructor parameters will not appear in the class lookup.
public class WeeklyOpportunityReminder implements Schedulable {
public void execute(SchedulableContext sc) {
List<Opportunity> closing = [
SELECT Id, Name, OwnerId
FROM Opportunity
WHERE IsClosed = false
AND CloseDate = NEXT_N_DAYS:7
LIMIT 200
];
List<Task> reminders = new List<Task>();
for (Opportunity opp : closing) {
reminders.add(new Task(
Subject = 'Opportunity closing soon: ' + opp.Name,
WhatId = opp.Id,
OwnerId = opp.OwnerId,
ActivityDate = Date.today().addDays(1)
));
}
insert reminders;
}
}This class has an implicit no-argument constructor, so Setup can schedule it. If you add a constructor that takes parameters, also add an explicit no-argument one, or schedule the class with System.schedule instead.
Creating the schedule
- Save the class and confirm it compiles.
- In Setup, enter Apex Classes in Quick Find and open it.
- Click Schedule Apex.
- Fill in the form and click Save.
The form asks for these values:
| Field | Meaning |
|---|---|
| Job Name | A label you choose. It must be unique among your scheduled jobs. |
| Apex Class | The Schedulable class to run. |
| Frequency | Weekly or Monthly. |
| Days or day of month | Which weekdays for Weekly. For Monthly, a date such as the 1st, or a pattern such as the last Friday. |
| Start and End | The date range in which the job may run. |
| Preferred Start Time | The time of day you would like the job to start. |
What "preferred" means
The start time is a preference. The platform queues the job at that time, but it can start later if system resources are busy. Do not build logic that depends on the job running at an exact second.
How Setup options map to cron
Setup stores the same kind of cron schedule that System.schedule uses. These examples show the equivalent expressions.
| Setup choice | Equivalent cron expression |
|---|---|
| Weekly, Monday to Friday, 6:00 AM | `0 0 6 ? * MON-FRI` |
| Monthly, on the 1st, 2:00 AM | `0 0 2 1 * ?` |
| Monthly, last Friday, 5:00 PM | `0 0 17 ? * 6L` |
Setup can only express daily-or-slower patterns. It cannot run a job every hour or every 15 minutes. For that, call System.schedule with a cron expression, or schedule several jobs from code.
// Hourly, which the Setup form cannot express
System.schedule('Reminders hourly', '0 0 * * * ?', new WeeklyOpportunityReminder());Viewing and managing the job
After you save, go to Setup and open Scheduled Jobs (search for it in Quick Find). The list shows each job's name, submitter, next run time and previous run time. Use Del to remove a job. Jobs created with System.schedule appear in the same list, so you can manage all of them in one place.
Common errors
- The class is missing from the lookup. It does not implement
Schedulable, it has no usable no-argument constructor, or it does not compile. - Duplicate job name. Choose a name that is not already in use.
- Changes to the class are blocked. Salesforce can refuse to save or deploy a class while jobs that use it are pending. Org deployment settings can allow deployments while Apex jobs are pending or in progress. Otherwise, delete the job, deploy, then schedule it again.
- Wrong time of day. Times use the time zone of the user who schedules the job. Check this before assuming the job misfired.
Exercise
A team wants a Schedulable class to run every 30 minutes. A colleague suggests the Setup form. Is that possible, and what should the team do?
Show answer
No. The Setup form offers only weekly and monthly frequencies, so it cannot run a job every 30 minutes. The team should call System.schedule from code. A cron expression cannot express "every 30 minutes" with a single minutes field of */30 in every case, so they can schedule two jobs, one with 0 0 * * * ? and one with 0 30 * * * ?, using different job names.