0

Is there always a need to do the kill cron job after scheduling it?

Instead can I just schedule a one time job?

If so, what is the difference in syntax between a recurring job and a one time job?

2 Answers 2

1

You can schedule a job with a specific set cron string, e.g. 0 0 0 21 MAY ? 2022 to run something exactly at midnight tonight (at the time of this answer). After the job runs, it will be flagged as Completed and will not run again nor count against the 100 scheduled jobs governor limit. It is otherwise identical to a recurring job, but will contain no wildcards except for "day of week", which will be set to "?" ("do not care"). If you want to schedule a one-off batch job, you can also use System.scheduleBatch, which schedules a batch job to run after a specified number of minutes, and never again.

3
  • When you say "otherwise identical", what do you mean? What are the differences between the syntax for the recurring job and the one-time job? Commented May 20, 2022 at 21:28
  • 1
    A word of warning for close-in scheduling of one-off jobs; see this posting.
    – Phil W
    Commented May 21, 2022 at 8:10
  • 1
    The difference in syntax for a one-off job is that you do not use wildcards in the date/time specification.
    – Phil W
    Commented May 21, 2022 at 9:34
0

I realize this has been answered for sometime, but I think my findings will be useful to others and are too long to fit into a comment.

I found that running System.schedule with a cron string defined like 0 0 0 21 MAY ? 2022 actually schedules the job to run once per year. That is, when you specify a specific year value versus a year range. Looking at the SF docs and Quartz docs it seems like passing a single year value shouldn't be supported at all, but this is the behavior I've observed.

The good news is that System.scheduleBatch works as desired. However only for batch classes and not schedulable classes. In my case, I needed to dynamically instantiate various job types, both schedulable and batchable. I was happy to find that casting to (Database.Batchable<SObject>) is possible in my scenario and allows for use of the scheduleBatch method with reflection & dynamic types. Here is a toy example:

public class myWrapper implements Schedulable {
  // does important stuff
}

public class myScheduler {
  public static void runNow(String name) {
    Type curBatchType = Type.forName(name);
    myWrapper myJob = (myWrapper) curBatchType.newInstance();

    if (myJob instanceof Database.Batchable<SObject>) {
      System.scheduleBatch((Database.Batchable<SObject>)myJob, 'Job Name', 1); 
    }
  }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .