Schedule Jobs Using the Apex Scheduler
Learning Objectives
Follow Along with Trail Together
Want to follow along with an instructor as you work through this step? Take a look at this video, part of the Trail Together series on Trailhead Live.
(This clip starts at the 1:09:53 minute mark, in case you want to rewind and watch the beginning of the step again.)
Scheduled Apex
The Apex Scheduler lets you delay execution so that you can run Apex classes at a specified time. This is ideal for daily or weekly maintenance tasks using Batch Apex. To take advantage of the scheduler, write an Apex class that implements the Schedulable
interface, and then schedule it for execution on a specific schedule.
Scheduled Apex Syntax
To invoke Apex classes to run at specific times, first implement the Schedulable
interface for the class. Then, schedule an instance of the class to run at a specific time using the System.schedule()
method.
The class implements the Schedulable
interface and must implement the only method that this interface contains, which is the execute()
method.
The parameter of this method is a SchedulableContext
object. After a class has been scheduled, a CronTrigger
object is created that represents the scheduled job. It provides a getTriggerId()
method that returns the ID of a CronTrigger
API object.
Sample Code
This class queries for open opportunities that should have closed by the current date, and creates a task on each one to remind the owner to update the opportunity.
You can schedule your class to run either programmatically or from the Apex Scheduler UI.
Using the System.Schedule Method
After you implement a class with the Schedulable
interface, use the System.schedule()
method to execute it. The System.schedule()
method uses the user's timezone for the basis of all schedules, but runs in system mode—all classes are executed, whether or not the user has permission to execute the class.
The System.schedule()
method takes three arguments: a name for the job, a CRON expression used to represent the time and date the job is scheduled to run, and an instance of a class that implements the Schedulable
interface.
For more information on the CRON expression used for scheduling, see the “Using the System.Schedule Method” section in Apex Scheduler.
Scheduling a Job from the UI
- From Setup, enter
Apex
in the Quick Find box, then select Apex Classes. - Click Schedule Apex.
- For the job name, enter something like
Daily Oppty Reminder.
- Click the lookup button next to Apex class and enter
*
for the search term to get a list of all classes that can be scheduled. In the search results, click the name of your scheduled class. - Select
Weekly
orMonthly
for the frequency and set the frequency desired. - Select the start and end dates, and a preferred start time.
- Click Save.
Testing Scheduled Apex
Just like with the other async methods we’ve covered so far, with Scheduled Apex you must also ensure that the scheduled job is finished before testing against the results. To do this, use startTest()
and stopTest()
again around the System.schedule()
method, to ensure processing finishes before continuing your test.
Things to Remember
- You can only have 100 scheduled Apex jobs at one time and there are maximum number of scheduled Apex executions per a 24-hour period. See Execution Governors and Limits in the Resources section for details.
- Use extreme care if you’re planning to schedule a class from a trigger. You must be able to guarantee that the trigger won’t add more scheduled jobs than the limit.
- Synchronous Web service callouts are not supported from scheduled Apex. To be able to make callouts, make an asynchronous callout by placing the callout in a method annotated with
@future(callout=true)
and call this method from scheduled Apex. However, if your scheduled Apex executes a batch job, callouts are supported from the batch class.