Salesforce Apex Scheduler

  • To invoke Apex classes to run at specific times, first implement the Schedulable interface for the class. Then, schedule an instance of the class to run at a specific time using the System.schedule method.
  • The class implements the Schedulable interface and must implement the only method that this interface contains, which is the execute method.
  • The parameter of this method is a SchedulableContext object. After a class has been scheduled, a CronTrigger object is created that represents the scheduled job. It provides a getTriggerId method that returns the ID of a CronTrigger API object.

Sample Code:

global class AccountBatchScheduled implements Schedulable {
    global void execute(SchedulableContext ctx) {
        //Batch class scheduled using Schedulable interface
        Database.executebatch(new AccountBatch());
    }
}

There are 2 ways to schedule an apex job:

  • Scheduling a Job from the UI
  • Using the System.Schedule Method