How to Schedule Batch Apex for Every 5 Minutes?

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.

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.


					
				

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


					
				

Here is the explanation of the above cron expression

  1. 0 Seconds
  2. 00 Minute
  3. Every Hour
  4. Every Day
  5. Every week

Schedule Apex Class

To Schedule the Apex Class, You need to execute the below piece of code from the Developer Console.


					
				

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
Amit Singh
Amit Singh

Amit Singh aka @sfdcpanther/pantherschools, a Salesforce Technical Architect, Consultant with over 8+ years of experience in Salesforce technology. 21x Certified. Blogger, Speaker, and Instructor. DevSecOps Champion

Articles: 299

Newsletter Updates

Enter your email address below and subscribe to our newsletter

2 Comments

  1. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Discover more from Panther Schools

Subscribe now to keep reading and get access to the full archive.

Continue reading