Similar to the scheduled trigger flow in Salesforce, when we want to set a specific time to execute an Apex class, we have a Scheduler Apex in Salesforce that we can use to schedule the Apex class.
In this tutorial, we will learn about the Apex scheduler in Salesforce, with examples. In that, I will explain what Scheduler Apex is in Salesforce, its methods, and how to use a Scheduler Apex in Salesforce.
What is Scheduler Apex in Salesforce?
The schedule apex is a type of asynchronous apex that enables the automated execution of Apex classes at specified times. By implementing the Schedulable interface in an Apex class, we can define daily operations, such as data cleanup or batch processing.
This feature ensures that critical tasks are performed regularly without manual intervention, enhancing efficiency.
It is very useful to perform daily tasks such as data cleanup or any other logic that needs to be run at a particular time. We can call batch apex inside the scheduler to perform operations on large data sets to avoid hitting the governor limit.
Syntax: Scheduler Apex
global class Class_Name implements Schedulable {
global void execute ( SchedulableContext SC ) {
}
}- Schedulable: In Salesforce, the Apex class implements the schedulable interface, which allows it to be scheduled for execution at a specific time.
- Global: The class must be global because it implements the schedulable interface, and Salesforce requires scheduled Apex classes to be accessible system-wide.
- Execute(): is the required method when implementing schedulable.
- SchedulableContext SC: This parameter provides context about the scheduled execution, but it is rarely used.
When to Use Salesforce Apex Scheduler?
- The scheduler apex is used to execute tasks at a specified time. We can automate periodic tasks, such as batch processing.
- We can run maintenance jobs to reduce the number of records displayed in the object, such as archiving old records.
- Additionally, we can schedule reports or send notifications using the Scheduler Apex.
Example: Create a Scheduler Apex in Salesforce
Now, I will explain step by step how to create a scheduler apex by implementing the schedulable interface and how to execute it in Salesforce.
For example, if any opportunities that were closed 50 days ago, then we need to delete all those opportunities. After that, every week, it should check if any opportunity was closed 50 days ago, then the apex class should execute, and the opportunity should be deleted.
Before creating the scheduler apex, let’s check the opportunities that were closed 50 days ago. Now, we want to delete these opportunities.

Here, I created the Apex class DeleteOpportunities, a scheduled Apex class implemented to delete closed opportunities that were closed within the last 50 days.
global class DeleteOpportunities implements Schedulable {
public void execute( SchedulableContext ctx ) {
List<Opportunity> oppoList = [ SELECT Name, StageName, CloseDate FROM
Opportunity WHERE IsClosed = True AND CloseDate = LAST_N_DAYS:50 ];
try{
if( oppoList != null && oppoList.size() > 0) {
DELETE oppoList;
}
}
catch( Exception e ){
System.debug( 'Error occurred while deleting opportunities ' +e.getMessage());
}
}
}Now, let’s understand the implementation of the scheduled apex class. Below, I have explained step by step how we implemented and executed the apex class.
In the above apex class, we declared it as global and implemented the Schedulable interface. In that, we declared the execute() method, which is required when implementing Schedulable. Then, the system calls this method when the scheduled job runs.
In the method, we declared a list collection that retrieves closed opportunities (IsClosed = True) and has a CloseDate within the last 50 days.
After that, using exception handling (try, catch blocks), we checked whether the list had records and deleted all records that were in it. If any error occurs, the catch block handles the error and logs an error message if something goes wrong.
How to Execute Scheduler Apex in Salesforce
There are two ways to execute the Salesforce Apex scheduler. Below, I have explained both options for scheduling our apex:
- Schedule Apex Class from Salesforce UI
- Using System.Schedule() Method in Apex.
1. Schedule Apex Class from Salesforce UI
In the following steps, I will explain how to schedule an Apex class from the Salesforce Lightning UI.
- Go to Setup -> in Quick Find, search for ‘Apex Classes‘. -> Click on it.

- In the Apex Classes, click the Schedule Apex button to execute the Apex class at a specific time interval.

- To schedule the apex class, we need to provide the following details:
- Job Name: Provide the job name. It should be in API name format.
- Apex Class: Select the apex class that you want to schedule.
- Schedule Using: Here, we have selected a schedule builder.
- Frequency: There are two options to choose a frequency.
- Weekly: If you select ‘Weekly’, you must select a day from the week so that the class will execute on that chosen day.
- Monthly: Here, you need to select any days from the month. So that on the chosen day, the class can execute.
- Start Date: Select the start date from when you want to schedule the apex class.
- End Date: The end date will be the last date to execute the scheduled apex.
- Preferred Start Time: Select the time so the class executes at the selected time and date.
After that, click the Save button to schedule the apex class.

- To view the scheduled Apex class in the Quick Find, search for the Apex Classes. Click on it. In the image below, you can see the class we selected in the schedule apex.
- Then, in the Job Type, we have Scheduled Apex.

Again, as you write a query to retrieve the records that were closed 50 days ago, then you will get nothing because, using the scheduler ape,x, we already deleted those records.

2. Using System.Schedule() Method in Apex
Another way of scheduling the job is by programmatically executing an anonymous window in the developer console. We have to use the System.Schedule method to execute our Apex. It takes three parameters: job name, CRON expression, which represents frequency, and class instance.
Syntax:
DeleteOpportunities DelOppo = new DeleteOpportunities ();
String str = ' 0 0 12 ? * FRI ' ;
String jobID = System.schedule( ' Delete closed opportunities ', str, DelOppo );The above expression will schedule an Apex class every Friday at 12:00 p.m. We can also monitor our job by navigating to the Scheduled Job in the Quick Find box from Setup.
In the image below, we can see that our Next Scheduled Run is on 14 February, which is a Friday this year(2025).

Conclusion
I hope now you have got an idea about the scheduler apex in Salesforce with examples. In that, I have explained what scheduler apex is in Salesforce, its syntax, and when to use it. After that, using the scenario, I explained how to create a scheduler apex and execute it from the scheduler job and system.Schedule() method in Apex.
You may like to read:
- Queueable Apex Chaining in Salesforce With Examples
- Create and Use Constructors in Salesforce Apex Classes
- Batch Apex in Salesforce With Examples
- Handle Events in Salesforce Lightning Web Components
- Create and Send PDF Via Email Using Salesforce Flow
I am Bijay Kumar, the founder of SalesforceFAQs.com. Having over 10 years of experience working in salesforce technologies for clients across the world (Canada, Australia, United States, United Kingdom, New Zealand, etc.). I am a certified salesforce administrator and expert with experience in developing salesforce applications and projects. My goal is to make it easy for people to learn and use salesforce technologies by providing simple and easy-to-understand solutions. Check out the complete profile on About us.