Salesforce Cookies #3

If there is a need of scheduling an apex job or schedulable apex class after each hour then cron expression is the best way. Cron expression allows you to schedule a job on a specific time. Following cron expression runs a job after each hour of the day.

scheduledJob j = new scheduledJob();
String sch = '0 0 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 ? * *';
System.schedule('My Job', sch, j);
Cron expression is built with following parameters - 
Seconds Minutes Hours Day_of_month Month Day_of_week Optional_year

You can Special Characters in a Cron expression such as running a job only on weekdays after each 4 hours then following Cron expression will be used.

scheduledJob j = new scheduledJob();
String sch = '0 0 0,4,8,12,16,20 ?  MON-FRI';
System.schedule('My Job', sch, j);

For more details you can check the Salesforce Article.

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.