Introduction
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.
Create Simple Scheduler Class
To Schedule any apex class to run at particular interval first we need an apex class which is implementing System.Schedulable interface.
Use below code for sample class.
global class YourScheduler implements System.Schedulable{
global void execute(SchedulableContext SC){
/* Call Batch Apex Here or any logic */
}
}
Prepare Cron Expression
The very first step is to prepare the CRON Expression so that we can tell the system to run at particular interval.
Here is the simple expression which will run at 60 minute of an hours
0 00 * * * ?
Here is the explanation of the above cron expression
- 0 Seconds
- 00 Minute
- Every Hour
- Every Day
- Every week
Schedule Apex Class
To Schedule the Apex Class, You need to execute the below piece of code from the Developer Console.
System.schedule('YourScheduler 1', '0 00 * * * ?', new YourScheduler());
System.schedule('YourScheduler 2', '0 05 * * * ?', new YourScheduler());
System.schedule('YourScheduler 3', '0 10 * * * ?', new YourScheduler());
System.schedule('YourScheduler 4', '0 15 * * * ?', new YourScheduler());
System.schedule('YourScheduler 5', '0 20 * * * ?', new YourScheduler());
System.schedule('YourScheduler 6', '0 25 * * * ?', new YourScheduler());
System.schedule('YourScheduler 7', '0 30 * * * ?', new YourScheduler());
System.schedule('YourScheduler 8', '0 35 * * * ?', new YourScheduler());
System.schedule('YourScheduler 9', '0 40 * * * ?', new YourScheduler());
System.schedule('YourScheduler 10', '0 45 * * * ?', new YourScheduler());
System.schedule('YourScheduler 11', '0 50 * * * ?', new YourScheduler());
System.schedule('YourScheduler 12', '0 55 * * * ?', new YourScheduler());
Enhancement
Now you can change the above CRON Expression to run the batch for following minutes.
- 10 Minutes
- 20 Minutes
- 30 Minutes
- 15 Minutes
- 25 Minutes
- etc
Thanks for the post . Is there any practical scenario for such frequent action. Also in such frequent operation Salesforce limit of 100 scheduler in a org might reach soon?
Hi Manish,
I have come across multiple requests from the clients regarding the same. However, it’s always best to say no with some other options for example calling the same batch again from the finish after 5 min. (There is a method inside the System class that can schedule the batch to execute after certain minutes ).
Yes the limit will exceed soon, so we need to keep in mind while scheduling these frequent batch apex.