Hello Everyone, In this post, We are going to see a code example that illustrate you, how you can schedule an apex class in every 5 or in 10 minutes duration in salesforce using CRON expression. This is too simple, so without wasting time let's get started,

Schedule Apex in every 5 or 10 minutes in Salesforce

In apex, By default scheduled job run in every 1 hour using CRON expression but you can scheduled this job in every 5 minutes or 10 minutes duration. Scheduled a class in every 5 minutes or 10 minutes is not possible to do through the standard Salesforce user interface, But you can achieve this by using a small piece of code. So here are the steps,

1. Navigate to Developer Console | New | Apex Class, create a new apex class “SampleScheduler” and replace the following markup in the class.

 global class SampleScheduler implements Schedulable{
global void execute(SchedulableContext SC){
//Code logic goes here..
}
}
2. To schedule class in every 5 minutes, Navigate to Developer Console | Debug | Open Execute Anonymous Window. Copy and paste the below code and click on “Execute” button.
 System.schedule('SampleScheduler 1',  '0 00 * * * ?', new SampleScheduler());
System.schedule('SampleScheduler 2', '0 05 * * * ?', new SampleScheduler());
System.schedule('SampleScheduler 3', '0 10 * * * ?', new SampleScheduler());
System.schedule('SampleScheduler 4', '0 15 * * * ?', new SampleScheduler());
System.schedule('SampleScheduler 5', '0 20 * * * ?', new SampleScheduler());
System.schedule('SampleScheduler 6', '0 25 * * * ?', new SampleScheduler());
System.schedule('SampleScheduler 7', '0 30 * * * ?', new SampleScheduler());
System.schedule('SampleScheduler 8', '0 35 * * * ?', new SampleScheduler());
System.schedule('SampleScheduler 9', '0 40 * * * ?', new SampleScheduler());
System.schedule('SampleScheduler 10', '0 45 * * * ?', new SampleScheduler());
System.schedule('SampleScheduler 11', '0 50 * * * ?', new SampleScheduler());
System.schedule('SampleScheduler 12', '0 55 * * * ?', new SampleScheduler());

3. To schedule class in every 10 minutes repeat step 2 with following CRON expression.

 System.schedule('SampleScheduler 1', '0 0 * * * ?', new SampleScheduler());
System.schedule('SampleScheduler 2', '0 10 * * * ?', new SampleScheduler());
System.schedule('SampleScheduler 3', '0 20 * * * ?', new SampleScheduler());
System.schedule('SampleScheduler 4', '0 30 * * * ?', new SampleScheduler());
System.schedule('SampleScheduler 5', '0 40 * * * ?', new SampleScheduler());
System.schedule('SampleScheduler 6', '0 50 * * * ?', new SampleScheduler());

If you want to check apex class in scheduled or not then Navigate to Setup | Jobs | Scheduled Jobs.


 The following are some example of CRON expressions:

ExprssionDescription
0 0 13 * * ?Run every day at 1 PM.
0 0 22 ? * 6LRun the last Friday of every month at 10 PM.
0 0 0 ? * * *At 12:00 AM every day
0 0 10 ? * MON-FRIRun Monday through Friday at 10 AM.
0 0 20 * * ? 2024Run every day at 8 PM during the year 2024.
0 0 12 * * ?At 12:00 PM every day
0 0 10 ? * *At 10.00 AM every day
0 0 10 * * ?At 10.00 AM every day
0 0 10 * * ? *at 10.00 AM every day
0 0 15 ? * * *At 3:00 PM every day
0 30 * * * ?Every 30 minutes
0 0 17 ? * 6LRuns the last Friday of every month at 5:00 PM.
0 25 5 15 * ?At 5:25 AM on the 15th day of every month
0 15 17 ? * 6#3At 5:15 PM on the third Friday of every month
0 0-5 15 * * ?Every minute starting at 3:00 PM and ending at 3:05 PM, every day
0 15 17 ? * MON-FRIAt 5:15 PM every Monday, Tuesday, Wednesday, Thursday and Friday
0 0 23 * * ? 2022Runs every day at 11:00 PM during the year 2022.

Hope you like this post, for any query or suggestions please feel free to comment.

Thank you.