Admin Blogger Blogs Business Use Developer

Apex Schedulers and Scheduling Using CRON Expressions

Overview                – Blog holds the explanation of how to use apex schedulers.

Problem                  – It is usually the requirement that we need some automated process that can automatically keep running at one particular time every day or after few days gap into your SalesForce org.

Solution                  – The possible solution is that, we can use Apex Schedulers.
What is Scheduled Apex?
Currently in the universe of SalesForce, if you want to align your set of codes and you want them to run a set time every day or on a particular time set by you, then your answer is Scheduled Apex. Scheduled Apex is like a job that gets executed from your org whenever it is scheduled for.
For scheduling any apex code, all we need is to implement a Schedulable interface. This interface, when implemented contains only one ‘execute’ method which is mandatory to use.
Basic Structure of Scheduled Apex

After implementing your code in the Scheduled Apex, now the question is how we can schedule this Apex code. Well I will be sharing two ways to do it. Out of two ways one of the ways is a standard way and the other is a custom way to schedule an apex.
Standard method of Scheduling an Apex
Follow these steps to schedule an apex: –

  • Click on SETUP
  • In Quick Find/ Search box type ‘Apex Classes’ and click on the same
  • Then click on ‘Schedule Apex Button’

  • After clicking on the Schedule Apex button, you will see a page where you have to enter your scheduled job name, select Apex class to be scheduled, then select on what days you want to run your job – weekly/monthly, then enter the duration for how long your job will keep executing and what is preferred start time of your job.

  • After entering required details, click on save
  • Once saved, you can view your scheduled job. Click on setup, in search box, type ‘Scheduled Jobs’. Click on the same. You can view your scheduled job, when in was created, when it was executed last and when it will execute next.

One of the major limitations for scheduling an Apex using standard method is that, you will have to specify an end time for the job. You cannot make it run for an infinite time.
Other limitation is that, these jobs can get executed only after 1 hour. What if there is a requirement to execute a job every 5 minutes or after every half an hour. These cannot be achieved using standard method.
Custom method of Scheduling an Apex:
Here is another and a much better method of scheduling an Apex code. This custom way of scheduling apex uses CRON expressions. CRON Expressions are a statement that specifies the exact time when a particular job is to be scheduled.
The general format for a CRON Expression is:
Here is a sample:

Here the CRON expression specifies that, the scheduled job will run at 12:45 pm for every day for infinite times. This is one of the major advantages of using CRON’s over standard methods.
? – No Value
* – Include all Values
L – Last
Some of the samples CRON Expressions are:
‘0 0 7 ? * *‘                                                              Runs at 7:00 AM everyday
‘0 0 16 * * ?’                                                            Runs at 4:00 PM everyday
‘0 0 10 ? * WED’                                                    Runs at 10:00 AM every Wednesday
‘0 0 30 22 ? * MON-WED ’                                 Runs at 10:30 PM every Monday Tuesday Wednesday
Test Class for Apex Scheduler:

This test class gives 100% code coverage. For any extra code in Apex class, code in test class needs to be added.
This is how any Apex code can be scheduled using various methods and at different times as per your requirement. Using scheduled apex makes it is easy to handle jobs that you want to run in a SalesForce system.
Written By: Himanshu Tiwari
Managed Under: Kushal Mishra

Similar Posts