Scheduler Apex

                                           Scheduler Apex



What is Salesforce Scheduler?

Salesforce scheduler is one of the asynchronous methods in salesforce, which is used to execute classes at a specific time.

For example, update all the records of Account object at 12 AM daily or Send emails every day at 1AM. These kinds of operations we need to schedule. For that we use Scheduler Apex.

Note: We can schedule batch class, Queueable class or any code snippet.

Scheduling can be done in 2 different ways,
        1. Using user interface (UI). or
        2. Programmatically using CRON expression.

To invoke a class to run at specific time we need to implement Schedulable interface. then specify the scheduling time using either User Interface or by CRON expression using System.Schedule method.

Syntax:


Let's take a simple example,


In the above class we have written a code to update the description as ‘Created Today’ for the accounts that were created today.

Note: SchedulableContext Interface Represents the parameter type of a method in a class that implements the Schedulable interface and contains the scheduled job ID.

Now let's try to schedule TestScheduler class using both the ways (UI and CRON).

1) Using User Interface:

Navigate to your Org --> Setup --> In quick find type apex classes and select Apex classes --> select Schedule Apex.


Give a job name and select TestScheduler class (Only schedulable class will be available as options). Select your preferred time of execution. Click Save.














This is how we schedule apex class with the help of UI.


2) Using CRON Expression:

If we want to run any job several times a day. Lets say 5:30 AM, 9:30 AM and 5:30 PM. In this case we cannot use User Interface. This can be achieved using CRON Expression Programmatically.

Now Let's try to schedule job using CRON Expression,

Syntax,


CRON Expression Contains 7 parameters. Whereas the last one (Optional_year) is optional.

Seconds Minutes Hours Day_of_month Month Day_of_week Optional_year

Let's see an example to understand CRON expression.






In above example * means All values and ? means No Values.

Here I am scheduling a job at 0 sec, 0 min, 0 hour, All Day_of_the_month, All Months,No value, All years. Which means this job will run at 12 AM every day.

NOTE: You cannot pass Day_of_the_month and Day_of_week at the same time. 
Consider 0 0 0 3 FEB TUE * - What if 3rd day of Feb is not Tuesday?? 

Available value for each of the parameter,

Seconds

0 - 59

Minutes

0 - 59

Hours

0 - 23

Day_of_Month

1 - 31 or * or ?

Month

1 - 12 or JAN-DEC or * or ?

Day_of_week

1 - 7 or SUN-SAT or * or ?

Optional_year

1970 - 2099


Check out more examples for CRON Expression,

0 5 * * * ?  --> class runs every hour at 5 min past the hour.
0 10 5 ? * MON-FRI --> Class runs every Monday to Friday at 5:10 AM.
0 0 0 * 12 ? * --> Class runs every day only in the month of December.

NOTE: Apex doesn’t allow job to schedule more than once in the same hour.

Open execute anonymous window and write your CRON expression code. Click on execute.
Now your job has been scheduled.

If you want to schedule a job multiple times a day, you can write the code as below example.












Monitor the scheduled jobs:

Now that we have scheduled our job, Let's see where we can monitor them.

Open the org --> Setup --> In quick find type ‘Scheduled jobs’ and open it .Here you can see all your scheduled jobs. 









Points to Note:

  1. You can only have 100 scheduled jobs at one time.

  2. The System.Schedule method uses the user’s time zone for the basis of all Schedules.

Summary:
  1. We can schedule batch class, Queueable class or any other code snippet.
  2. Scheduling can be done in 2 ways, UI and CRON.
  3. We can monitor current and future scheduled jobs.


See also: Future method in salesforce


Authors:
                                                                            

Comments