Salesforce Apex Scheduler


In this blog post, we are going to see how to use salesforce apex scheduling capabilities which support invoking the Apex class at specific times like daily or weekly or even every minute. To invoke Apex classes to run at specific times, first implement the Schedulable interface for the class, then specify the schedule using either the Schedule Apex page in the Salesforce user interface or the System.schedule method.

Apex Syntax

To schedule an Apex class to run at regular intervals, first, write an Apex class that implements the Salesforce-provided interface Schedulable.The schedulable interface is having the only execute method.

global void execute(SchedulableContext sc){}

execute method will take the SchedulableContext object as an argument which will help to track the scheduled job once it’s scheduled. The SchedulableContext getTriggerID method returns the ID of the CronTrigger object associated with this scheduled job as a string. You can query CronTrigger to track the progress of the scheduled job.To stop the execution of a job that was scheduled, use the System.abortJob method with the ID returned by the getTriggerID method.Here is the sample code

global class scheduledApex implements Schedulable {
    global void execute(SchedulableContext SC) {
        DateTime dt =;
        Long timeinNumber = dt.getTime();
        Account a = new Account() ; 
        a.Name ='Account @'+String.valueOf(timeinNumber);
        insert a;

Scheduling Apex

You can schedule the salesforce apex by using System.Schedule method or by using Salesforce UI. The main difference is Salesforce UI won’t support the Schedule job in Seconds and minutes which can be done with System.Schedule.The System.Schedule method uses the user’s timezone on 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, an expression used to represent the time and date the job is scheduled to run, and the name of the class. This expression has the following syntax:

Seconds Minutes Hours Day_of_month Month Day_of_week Optional_year

The following are some examples of how to use the expression.

Expression Description
0 0 13 * * ? Class runs every day at 1 PM.
0 0 22 ? * 6L Class runs the last Friday of every month at 10 PM.
0 0 10 ? * MON-FRI Class runs Monday through Friday at 10 AM.
0 0 20 * * ? 2010 Class runs every day at 8 PM during the year 2010.

Below code shows how to schedule apex by using the system.schedule which will run every day at 1 PM.Run this code from execute anonymous window.

scheduledApex apexSch = new scheduledApex();
String sch = '0 0 13 * * ?';
system.schedule('scheduledApex_1', sch, apexSch);

Scheduling a Job from the UI

 You can also schedule a class using the user interface.

  1. Go to Apex Classes.
  2. Click Schedule Apex 
  3. Click Save.


After the Apex job has been scheduled you can track the schedule details from the Salesforce UI or by running a SOQL query on CronTrigger as shown below.

CronTrigger ct = 
    [SELECT TimesTriggered, NextFireTime
    FROM CronTrigger WHERE Id = :jobID];

The jobID variable holding the ID of the job. The System.schedule method returns the job ID. If you’re performing this query inside the execute method of your schedulable class, you can obtain the ID of the current job by calling getTriggerId on the SchedulableContext argument variable. Assuming this variable name is sc, the modified example becomes:

CronTrigger job = 
    [SELECT Id, CronJobDetail.Id, CronJobDetail.Name, CronJobDetail.JobType 
    FROM CronTrigger ORDER BY CreatedDate DESC LIMIT 1];


You can also get the job’s name and the job’s type from the CronJobDetail record associated with the CronTrigger record. To do so, use the CronJobDetail relationship when performing a query on CronTrigger. This example retrieves the most recent CronTrigger record with the job name and type from CronJobDetail.

CronJobDetail ctd = 
    [SELECT Id, Name, JobType 
    FROM CronJobDetail WHERE Id = :job.CronJobDetail.Id];

Test Class

private class scheduledApexTest {
    static testmethod void test() {
        Account a = new Account();
        a.Name = 'testscheduledApex';
        insert a;
        String jobId = System.schedule('testscheduledApex',
                                       '0 0 0 3 9 ? 2022' ,
                                       new scheduledApex());
        // Get the information from the CronTrigger API object
        CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, 
                          FROM CronTrigger WHERE id = :jobId];
        // Verify the expressions are the same
        System.assertEquals('0 0 0 3 9 ? 2022',ct.CronExpression);
        // Verify the job has not run
        System.assertEquals(0, ct.TimesTriggered);


Things to Remember

  1. Salesforce schedules the class for execution at the specified time. Actual execution may be delayed based on service availability.
  2. You can only have 100 scheduled Apex jobs at one time
  3. 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 classes than the limit.
  4. If there are one or more active scheduled jobs for an Apex class, you cannot update the class or any classes referenced by this class through the Salesforce user interface. However, you can enable deployments to update the class with active scheduled jobs by using the Metadata API (for example, when using the IDE
  5. Synchronous Web service callouts are not supported from scheduled Apex.
  6. The maximum number of scheduled Apex executions per a 24-hour period is 250,000 or the number of user licenses in your organization multiplied by 200, whichever is greater. This limit is for your entire org and is shared with all asynchronous Apex: Batch Apex, Queueable Apex, scheduled Apex, and future methods.
  7. Apex jobs scheduled to run during a Salesforce service maintenance downtime will be scheduled to run after the service comes back up, when system resources become available. If a scheduled Apex job was running when downtime occurred, the job is rolled back and scheduled again after the service comes back up.