Sub topic 3
Reading and writing cron expressions
This chapter assumes you can write a basic Apex class and know how System.schedule is called. You will learn to read a cron expression field by field and write expressions for common timetables.
The seven fields
A cron expression is a string of fields separated by spaces. Salesforce reads them in this order:
Seconds Minutes Hours Day_of_month Month Day_of_week [Year]The Year field is optional. The other six are required. Many developers know the Unix format, which has five fields and starts with minutes. If you paste a Unix expression into Salesforce, every value lands in the wrong field.
| Field | Allowed values |
|---|---|
| Seconds | 0-59 |
| Minutes | 0-59 |
| Hours | 0-23 (24-hour clock) |
| Day_of_month | 1-31 |
| Month | 1-12 or JAN-DEC |
| Day_of_week | 1-7 (1 is Sunday) or SUN-SAT |
| Year | 1970-2099 |
Special characters
Each field accepts the values above plus these characters.
| Character | Meaning | Example |
|---|---|---|
| `*` | Every value | `*` in Month means every month |
| `?` | No specific value (Day_of_month and Day_of_week only) | `?` in Day_of_week |
| `,` | A list of values | `MON,WED,FRI` |
| `-` | A range | `MON-FRI` |
| `/` | An increment: start/step | `0/15` in Minutes means 0, 15, 30, 45 |
| `L` | The last day | `L` in Day_of_month is the last day of the month |
| `W` | The nearest weekday | `15W` is the weekday closest to the 15th |
| `#` | The nth weekday of the month | `2#1` is the first Monday |
The ? character causes the most errors. Dayofmonth and Dayofweek both describe the day, so you must constrain only one of them. Put a value in one field and ? in the other. If you give both a value, or use * in both, scheduling fails.
Worked examples
Read the first example as: second 0, minute 0, hour 8, any day of the month, every month, Monday to Friday.
| Expression | Runs |
|---|---|
| `0 0 8 ? * MON-FRI` | At 8:00 AM on weekdays |
| `0 0 13 * * ?` | At 1:00 PM every day |
| `0 0/30 * * * ?` | Every 30 minutes, on the hour and half hour |
| `0 30 6 L * ?` | At 6:30 AM on the last day of each month |
| `0 0 9 ? * 2#1` | At 9:00 AM on the first Monday of each month |
| `0 0 0 1 1,4,7,10 ?` | At midnight on the first day of each quarter |
| `0 0 0 15 3 ? 2099` | Once, at midnight on March 15, 2099 |
To combine L and W, use LW in Dayofmonth. It means the last weekday of the month. Avoid using L or W inside lists or ranges, because the results are hard to predict.
Using an expression
Pass the string to System.schedule with a job name and a Schedulable instance. This example schedules the EscalateStaleCases class from earlier in the tutorial on two timetables.
String weekdayMorning = '0 0 8 ? * MON-FRI';
String monthEnd = '0 30 6 L * ?';
Id weekdayJob = System.schedule(
'Escalate stale cases - weekdays', weekdayMorning, new EscalateStaleCases());
Id monthEndJob = System.schedule(
'Escalate stale cases - month end', monthEnd, new EscalateStaleCases());
System.debug('Scheduled: ' + weekdayJob + ' and ' + monthEndJob);Each job needs a unique name. Scheduling the same class twice is allowed, as long as the names differ.
Time zone and timing
The job runs in the time zone of the user who scheduled it. If you schedule a job at 0 0 13 * * ? as a user in New York, it fires at 1:00 PM New York time. Daylight saving changes move the UTC time of the run, but the local time stays the same.
An expression gives the time the job is due. It does not promise the exact second the code starts. Salesforce queues the job and may start it a little late when the platform is busy. Do not write logic that depends on running at an exact second.
Common errors
- Wrong field order. A five-field Unix expression such as
0 8 * * 1-5is missing a field and has values in the wrong places. Always begin with seconds. - Both day fields set.
0 0 8 1 * MONfails. Use0 0 8 1 * ?for the first of the month, or0 0 8 ? * MONfor Mondays. - Day numbers off by one. Dayofweek starts at 1 for Sunday, not Monday. Names such as
MONavoid this mistake. - A 12-hour clock. Hours run 0-23, so 1:00 PM is
13. - Expecting a very fast schedule. Scheduled Apex is meant for timetables, not for polling every few seconds. If you need very frequent work, reconsider the design. A later chapter covers this.
Exercise
Write a cron expression that runs a job at 6:45 PM on the last Friday of every month. Hint: in Dayofweek, a number followed by L means the last occurrence of that weekday, and Friday is 6.
Show answer
0 45 18 ? 6L
Seconds is 0, minutes is 45, and hours is 18 (6 PM on a 24-hour clock). Dayofmonth is ? because Dayofweek is set. Month is `. Day_of_week 6L` means the last Friday.
Exercise
Why does the expression 0 0 8 * * MON fail to schedule, and how do you fix it for "every Monday at 8:00 AM"?
Show answer
Dayofmonth is * and Dayofweek is MON, so both day fields are constrained. One of them must be ?. The fix is 0 0 8 ? * MON.