Batch Processing
Apex Schedulable Apex
Using Schedulable Apex
Apex Schedulable Apex schedules jobs with System.schedule.
Introduction to Schedulable Apex
In Salesforce, Schedulable Apex allows developers to run Apex classes at specific times. This feature is particularly useful for automating tasks such as daily data processing or periodic updates. To schedule these jobs, Salesforce provides the System.schedule
method.
Understanding the System.schedule Method
The System.schedule
method is used to execute a schedulable class at a specific time. It requires three parameters:
- jobName: A string representing the name of the scheduled job.
- cronExpression: A string representing the schedule in a cron expression format.
- schedulableClass: An instance of a class that implements the
Schedulable
interface.
Implementing the Schedulable Interface
To create a schedulable Apex class, you need to implement the Schedulable
interface, which requires defining the execute
method. This method contains the logic that will run when the job is executed.
Scheduling a Job with System.schedule
Once you have a class that implements the Schedulable
interface, you can schedule it using the System.schedule
method. Below is an example of how to schedule a job to run every hour.
Cron Expression Format
Cron expressions are strings composed of six fields separated by spaces that represent time intervals. Here's a breakdown of the fields and their meanings:
- Seconds (0-59)
- Minutes (0-59)
- Hours (0-23)
- Day of month (1-31)
- Month (1-12 or JAN-DEC)
- Day of week (1-7 or SUN-SAT)
For example, the cron expression '0 0 12 * * ?'
schedules a job to run at noon every day.
Monitoring Scheduled Jobs
Salesforce provides tools to monitor scheduled jobs. You can view the status of scheduled jobs in the "Scheduled Jobs" page in Setup, where you can also abort jobs if needed.
Use Cases for Schedulable Apex
Schedulable Apex is ideal for scenarios where automation is needed at specific intervals, such as:
- Daily data exports
- Regular database cleanups
- Periodic integration with external systems
Batch Processing
- Batch Apex
- Queueable Apex
- Schedulable Apex
- Future Methods
- Previous
- Queueable Apex
- Next
- Future Methods