Skip to main content
Tech Expertise

Demystifying the CRON Expression in Salesforce

By Perficient Expert · · 3 min read
A Male Programmer Shows A Female Colleague A Coding Technique. The Codes Are Visible On The Laptop Screen

Hello, Trailblazers. In this blog post, we will cover a very important topic that we use in our day-to-day Coding practices or Programming- CRON Expression.

CRON Expression

As you know, Salesforce provides a very great feature of Scheduling an Apex class. You can schedule classes in a standard way through customization with the frequency of weekly days or monthly dates with a preferred start time. If you want to know how to do this, go with this link.

But with the help of CRON expression, we can schedule our apex class more customized.

In this blog post, we’ll learn what is CRON and CRON Expression. What is its use in Salesforce, how to write it, and many more.

So, let’s get started…

What is CRON?

CRON is a time-based job scheduling system commonly used in Unix-like operating systems. It enables users to schedule jobs (commands or scripts) to run on a regular basis at predefined times, dates, or intervals.

What is CRON Expression?

A CRON expression is a string representing a schedule that determines when a task or job should run. It is a standard syntax used in many operating systems, applications, and services for scheduling recurring tasks.

Sample Code:

SampleSchedulableClass testobj = new SampleSchedulableClass();
String cronexp = ‘0 0 5 ? * * *’;
System.schedule(‘Testing’, cronexp, testobj);

The above code executes the SampleSchedulableClass at 5.00 AM every day. Here, we stored the cron expression in the “cronexp” variable.

Use of Cron Expression in Salesforce:

In Salesforce, CRON expressions are primarily used for scheduling jobs, specifically in the context of scheduled Apex jobs. It allows developers to write a class that implements the Schedulable interface to run at specified intervals using CRON expressions.

For e.g.  System.Schedule(JobName, CronExpression, SchedulableClassName);

Syntax of CRON Expression:

The syntax of a CRON expression consists of fields that represent different aspects of time. Each field specifies a unit of time, and a CRON expression typically consists of six or seven fields, representing seconds, minutes, hours, day of month, month, day of week, and an optional year.

Here’s a Breakdown of a Standard Six-Field CRON Expression:

0305***
SecMinsHoursDay of MonthMonthDay of Week

In the above example of CRON Expression, a job will run every day at 5.30 in the morning.

The following are the allowed values, special characters, and requirements of the expression:

Name:Allowed Values:Special Characters:Required:
Seconds0-59NoneYes
Minutes0-59NoneYes
Hours0-23NoneYes
Day of Month1-31, - * ? / L WYes
Month1-12 or JAN-DEC, - * ? /Yes
Day of Week1-7 or SUN-SAT, - * ? / L #Yes
Year (Optional)Null or 1970-2099, - * ? /No

Special Characters in CRON Expression:

Special CharactersDescription
Comma (,)Separates individual values.
Hyphen (-)Specify a range of values.
Asterisk (*)It specifies all values.
Question Mark (?)It specifies no specific value.
Forward Slash (/)Specifies increments for ranges.
LRepresents the last day of the month or week.
WSpecifies the nearest weekday.
# (nth day_of_week)Specifies the nth occurrence of a day-of-week.

Note: You can use the L and W together to specify the last weekday of the month.

Here are Some Examples of CRON Expression:

ExpressionDescription
0 30 7 * * ? *Runs Every Day at 7:30 AM
0 0 8 ? * MON-FRI *Runs Every Weekday at 8 AM
0 0 * * * ? *Runs Every Hour
0 0/30 * * * ? *Runs Every 30 Minutes
0 0 0 1 * ? *Runs first day of month at Midnight
0 0 16? * SUN *Runs Every Sunday at 4 PM
0 0 3,6,9,12 * * ? *Runs at 3,6,9,12
0 0/5 9-17 ? * MON-FRI *Every 5 min 9AM-5PM weekdays

Conclusion:

With this, we learned what CRON expression is and covered all its aspects with the help of real-life examples.

Thus, I hope you enjoyed and understood CRON expression fairly. So, let’s take a reference from this post and build your Apex Schedulers according to your requirements. Stay Tuned!

References:

  1. Batch Apex in Salesforce

  2. Asynchronous Apex in Salesforce

You Can Also Read:

  1. An Introduction to Salesforce CPQ
  2. Salesforce CPQ and its Key Features
  3. Unlocking the Power of AI: Einstein for Developers
  4. Revolutionizing Customer Engagement: The Salesforce Einstein Chatbot

Perficient Expert

Perficient Experts break down complex technology with direct and practical insight, focusing on what works in the real world and how teams can move faster and build smarter.