Apex Cron expression considerations

Manjot Singh
Salesforce-Lightning
3 min readNov 17, 2023

Use case!

Sometimes we have some apex methods or batches which we need to run periodically, or we need to run them at a specific time. For these jobs we can use scheduled jobs with cron expressions. First let’s understand what is a cron expression. Cron expression has following parameters:-

Seconds, Minutes, Hours, Day_of_month, Month, Day_of_week, year(year is optional)

Here are few Examples of cron expressions: -

5 25 13 * * ? :- This cron expression means that given scheduled job will start on 1 pm 25 minutes and 5 seconds.

0 0 10 ? * MON-FRI 2023 :- This cron expression means that scheduled job will start on 10 am every Monday to Friday for year 2023.

0 0 10 1/3 * ? :- This cron expression means that job will start 1st of every month and then alternate every third day.

0 0 10 23 5 ? 2023 :- This is cron expression means that given schedule job will run on 23rd of May 2023 on 10 am.

You can see we are using few special characters in cron expressions. Their usage is given below:-

Cron gotcha for “/”

Cron expression makes scheduling jobs pretty easy. But there are few things that we need to keep in mind while creating crons using special character “/”. Let's say today is 5th of some month and we want to create a job that runs every third day and also runs today. So, we will create a cron expression like

0 0 10 5/3 * ?

This cron expression will run a job on 5th of this month on 10 am. then on 8,11,14,17,20,23,26,29.. Now we will be hoping that this job should run on 2nd of next month (Assuming this month is of 30 days). But our cron expression means that given expression starts every 5th of the month and then alternate 3 days. So, our jobs will run on 5,8,11,14,17,20,23,26,29 of this month then starts again on 5th of next month and will not run on 2nd of next month.

This is the first limitation not all times we can write a cron with / character that runs today also and after specified date. One solution will be to use below 2 cron expressions

0 0 10 1/3 * ?

0 0 10 */3 * ?

above 2 cron expressions will run on following days if month is 30 days 1,4,7,10,13,16,19,22,25,28,1,4,7

But if Current month is 31 days, then cron will run on 1,4,7,10,13,16,19,22,25,28,31,1,4,7. Here you can see that this job will run consecutively 31st and then 1st.

Another limitation of crons in Salesforce is that minimum time difference between jobs will be 1 hour. If we need to create any job that run every half an hour, we might need to create 2 different scheduled jobs.

Another thing to keep in mind that even if we have scheduled a job on specific time let's say 3:00 am it's not mandatory it will run on 3:00 am, job will be added to queue in salesforce and if there are any other jobs running in salesforce at that time it might take few minutes before our scheduled job starts.

You can always check whatever cron expressions you are creating on http://www.cronmaker.com/

If you learned something new…. Clap 👏 🙂

--

--