[SalesForce] Need System.schedule cron string for run once

Is there a way to use System.schedule to schedule a job that runs only once? I've been looking at the special characters allowed there, and I'm not seeing a way to do it.

What I want to do is use one scheduled job that runs once a week to spawn five separate run-once jobs that will check for missing attendance on specific days.

If scheduling a job to run-once isn't an option, is it possible to have a scheduled job that runs and kills other scheduled jobs?

Best Answer

You can do either option (both are viable).

To specify a run-once schedule, specify a fully qualified date and time, such as:

0 0 0 4 JUL ? 2014
| | | | |   | |
| | | | |   | \------- YEAR (2014)
| | | | |   \--------- DAY_OF_WEEK (NOT_SPECIFIED)
| | | | \------------- MONTH (JULY)
| | | \--------------- DAY_OF_MONTH (4th)
| | \----------------- HOUR (0- MIDNIGHT LOCAL TIME)
| \------------------- MINUTE (0)
\--------------------- SECOND (0)

This would run at midnight of the scheduling user on the Fourth of July this year, and never again.

If you want to kill other jobs, you simply query for scheduled jobs that are not already cancelled, and then call System.abortJob on those ID values. See CronTrigger for more information.

Finally, you can also abort the current job in the execute() method:

public void execute(SchedulableContext sc) {
    // Do stuff here
    System.abortJob(sc.getTriggerId());
}