Scheduling an Apex Job – Salesforce

Scheduling an Apex Job – Salesforce

To schedule Apex classes that run at specific times, first you need to create a class and then implement the Schedulable interface for the class, then specify the schedule using either the Schedule Apex page in the Salesforce user interface, or in the System.schedule method.

There are only two ways to schedule a apex job –

i) Scheduling a Job from the UI

ii) Using the System.Schedule Method

Apex Job Scheduling Syntax

global class MyScheduleClass implements Schedulable {
global void execute(SchedulableContext ctx) {
// some code here
}
}

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 CronTriggerobject is created that represents the scheduled job.

i) Scheduling a Job from the UI

You can schedule a class using the salesforce user interface. To schedule a class follow the below steps –

i) From Setup, enter Apex in the Quick Find box, then select Apex Classes.

ii) Click Schedule Apex.

iii) For the job name, enter something like MyScheduleClass.

iv) Click the lookup button next to Apex class and enter * for the search term to get a list of all classes that can be scheduled. In the search results, click the name of your scheduled class.

v) Select Weekly or Monthly for the frequency and set the frequency desired.

vi) Select the start and end dates, and a preferred start time.

vii) Click Save.

Job-Schedule

ii) Using the System.Schedule Method

You can also schedule a class using the System.Schedule method present inside the schedulable class

global class MyScheduleClass implements Schedulable {

    global void execute(SchedulableContext ctx) {
    MyScheduleClass msc = new MyScheduleClass(); 
    // Seconds, Minutes, Hours, Day of month, Month, Day of week, Optional year 
    String sch = '25 40 6 10 2 ?'; 
    String jobID = System.schedule('MyScheduleClass Job Run', sch, msc);

}
}

Cron Syntax:

Seconds, Minutes, Hours, Day of month, Month, Day of week, Optional year

Seconds : 0 – 59
Minutes  : 0 – 59
Hours     : 0 – 23
Day        : 1 – 31
Month    : 1 – 12
Week     : 1 – 7(Sunday is first and Saturday is last)
Year       : upto 2099

Cron Statement Examples: –

String cron = ‘0 59 * * * *’

The above corn statement executes MyScheduleClass for every one hour.

0 0 14 * * ?  –  Class runs every day at 2 PM.

0 0 22 ? * 6L  – Class runs the last Friday of every month at 10 PM.

0 0 10 ? * MON-FRI – Class runs Monday through Friday at 10 AM.

0 0 21 * * ? 2017 – Class runs every day at 9 PM during the year 2017.

? – No value
* – All values
L – Last
W – Nearest weekday

Some Limitations:

i) You can only have 100 scheduled Apex jobs at one time.
ii) Only Asynchronous Web service callouts are not supported from scheduled Apex.
iii) Maximum number of batch Apex jobs queued or active concurrently is 5.