Tag Archives: Apex Scheduler

Schedule An Apex Job To Run Every 5 Minutes

Sample Code:

YourScheduledApexClass obj = new YourScheduledApexClass();

String sch1 = '0 0 * * * ?';
System.schedule('Schedule Job1', sch1, obj);

String sch2 = '0 5 * * * ?';
System.schedule('Schedule Job2', sch2, obj);

String sch3 = '0 10 * * * ?';
System.schedule('Schedule Job3', sch3, obj);

String sch4 = '0 15 * * * ?';
System.schedule('Schedule Job4', sch4, obj);

String sch5 = '0 20 * * * ?';
System.schedule('Schedule Job5', sch5, obj);

String sch6 = '0 25 * * * ?';
System.schedule('Schedule Job6', sch6, obj);

String sch7 = '0 30 * * * ?';
System.schedule('Schedule Job7', sch7, obj);

String sch8 = '0 35 * * * ?';
System.schedule('Schedule Job8', sch8, obj);

String sch9 = '0 40 * * * ?';
System.schedule('Schedule Job9', sch9, obj);

String sch10 = '0 45 * * * ?';
System.schedule('Schedule Job10', sch10, obj);

String sch11 = '0 50 * * * ?';
System.schedule('Schedule Job11', sch11, obj);

String sch12 = '0 55 * * * ?';
System.schedule('Schedule Job12', sch12, obj);

Schedule An Apex Job To Run Every 15 Minutes

Sample Code:

YourScheduledApexClass obj = new YourScheduledApexClass();

String sch1 = '0 0 * * * ?';
System.schedule('Schedule Job1', sch1, obj);

String sch2 = '0 15 * * * ?';
System.schedule('Schedule Job2', sch2, obj);

String sch3 = '0 30 * * * ?';
System.schedule('Schedule Job3', sch3, obj);

String sch4 = '0 45 * * * ?';
System.schedule('Schedule Job4', sch4, obj);

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