Salesforce Fact #363 | Schedule batch apex using simple CRON expression

We can use CRON expression to schedule a batch apex to run at particular hour intervals.

Do you know that you can use shorter expression instead of specifying the exact hours. 

Suppose, we have to schedule a batch apex to run every 3 hours MON-FRI starting 12 am from a particular day. Now, it can be scheduled in two ways:

/*Specifying each hour value; long syntax

TestBatchSchedule tbs = new TestBatchSchedule();

String sch = '0 0 0,3,6,9,12,15,18,21 ? * MON-FRI';

System.schedule('test job', sch, tbs);


/*Specifying relative hour value; simple syntax

TestBatchSchedule tbs = new TestBatchSchedule();

String sch = '0 0 */3 ? * MON-FRI';

System.schedule('test job', sch, tbs);

Note: However, it is not possible to put similar kind of syntax for seconds and minutes. Trying to do so will give error: 'System.StringException: Seconds and minutes must be specified as integers'

Comments

Popular posts from this blog

Salesforce Fact #192 | Call batch apex from flow

Salesforce Fact #457 | Get current user role name in flow