Saturday, January 1, 2011

Scheduling Apex Class using System.schedule method

To schedule an Apex class, implements the Salesforce.com-provided interface Schedulable in your Apex class

example
 global class newScheduledClass implements Schedulable  
{

}


now use the following system.schedule method for above class
open system log (click Your Name | Setup) and execute the below mentioned method so it will get scheduled.

 newScheduledClass m = new newScheduledClass();  
String s = '1 30 1 10 2 ?';
system.schedule('Job Name', s, m);


here string s is in following pattern:
seconds, minutes, hours, dayofMonth, month, dayofWeek, year(optional)











































Values for expression:
Name Values Special Characters
Seconds 0–59 None
Minutes 0–59 None
Hours 0–23 , - * /
Day_of_month 1–31 , - * ? / L W
Month 1–12 , - * /
Day_of_week 1–7 , - * ? / L #
optional_year null or 1970–2099 , - * /




The special characters are defined as follows:
Special Character Description
, Delimits values. For example, use JAN, MAR, APR to specify more than one month.

- Specifies a range. For example, use JAN-MAR to specify more than one month.

* Specifies all values. For example, if Month is specified as *, the job is scheduled for every month.

? Specifies no specific value. This is only available for Day_of_month and Day_of_week, and is generally used when specifying a value for one and not the other.

/ Specifies increments. The number before the slash specifies when the intervals should begin, and the number after the slash is the interval amount. For example, if you specify 1/5 for Day_of_month, the Apex class runs every fifth day of the month, starting on the first of the month.

L Specifies the end of a range (last). This is only available for Day_of_month and Day_of_week. When used with Day of month, L always means the last day of the month, such as January 31, February 28 for leap years, and so on. When used with Day_of_week by itself, it always means 7 or SAT. When used with a Day_of_week value, it means the last of that type of day in the month. For example, if you specify 2L, you are specifying the last Monday of the month. Do not use a range of values with L as the results may be unexpected.

W Specifies the nearest weekday (Monday-Friday) of the given day. This is only available for Day_of_month. For example, if you specify 20W, and the 20th is a Saturday, the class runs on the 19th. If you specify 1W, and the first is a Saturday, the class does not run in the previous month, but on the third, which is the following Monday. Tip: Use the L and W together to specify the last weekday of the month.

# Specifies the nth day of the month, in the format weekday#day_of_month. This is only available for Day_of_week. The number before the # specifies weekday (SUN-SAT). The number after the # specifies the day of the month. For example, specifying 2#2 means the class runs on the second Monday of every month.


you can monitor the scheduled Apex job by going to
click Your Name | Setup | Monitoring | Scheduled Jobs


for more info:
Salesforce link

4 comments:

TristamJ said...

Awesome post. This should be in the APEX Developer Guide !

Angelika said...

For this code:

newScheduledClass m = new newScheduledClass(); String s = '1 30 1 10 2 ?'; system.schedule('Job Name', s, m);

What does 'm' represent?

Anonymous said...

@angelika

Here 'm' stands for a object which you want to execute/run the schedule apex's class

Jason said...

0 0 0 ? * * means every month every day of the week at midnight. Any idea why the the 'manage' link on the UI doesn't show up for this configuration? It's definitely something that can be represented by the controls on the UI.