Queueable Apex is one of four asynchronous Apex methods. Released by Salesforce in 2015, it combines the simplicity of Future Methods with the power of Batch Apex. As with all asynchronous methods, Queueable Apex runs in the background, in its own thread, at a later time.

Queueable Apex has the ability to (1) start a long-running operation and get an ID for it, (2) pass complex data types like sObjects to a job, and (3) chain jobs in a sequence.

Unlike other methods, Queueable Apex has an interface that allows developers to add jobs to the queue and monitor them. Each queued job runs when system resources become available. One benefit of using the interface is that some governor limits are higher than for synchronous Apex, such as heap size limits. However, keep in mind that you can add only up to 50 jobs within a single transaction.

Another huge benefit of Queueable Apex is the ability to chain jobs in a sequence. You can chain one queueable job to another by starting the second job from a running job. There are no limits on the depth of chained jobs. However, while chaining jobs, only one child job can exist for each parent job. 

Can We Schedule Queueable Apex?

The short answer is “no.” You cannot schedule Queueable Apex directly. If you want any delay on your job, it must follow the Scheduled Apex route. 

However, there are several workarounds which I will explain next.

Image depicting a puzzle piece being placed in a puzzle

Workarounds for Scheduling a Queueable Class in Salesforce

Scheduling a Queueable class involves the implementation of multiple interfaces. To explain three different workarounds, we enlisted the help of Brian Fear, Salesforce MVP, ranked number one in the Salesforce Stack Exchange.

Example of code for workaround for queueable Apex

Source: Brian Fear

Just adding a few lines of code makes a Queueable class available in both the user interface for scheduling, as well as System.scheduleJob.

Example of code for workaround to queueable Apex

Source: Brian Fear

As shown in Workaround #2, we can also schedule a Queueable class dynamically for use in a Scheduled Flow or Scheduled Path.

Example of workaround for queueable Apex

Source: Brian Fear

In Workaround #3, we provide a way to pass in additional parameters and use the Callable interface. This isn’t compatible with the user interface for scheduling Apex, but would be accessible to flows.

Example of workaround for queueable Apex

Source: Brian Fear

In Step 2 of Workaround #3, we pass in arbitrary parameters to an arbitrary class that supports Callable and Queueable.

What Other Types of Scheduled Functions Are There in Salesforce?

There are three other types of scheduled functions (aka asynchronous Apex) in Salesforce: Scheduled Apex, Batch Apex, and Future Methods. As with Queueable Apex, each type has its own unique benefits and use cases.

Scheduled Apex

Perfect for daily or weekly tasks, Scheduled Apex only runs on the records you want, when you want. This function is triggered by a specific schedule and not by user actions. 

Batch Apex

Due for a clean up? Batch Apex is best for cleansing or archiving a large quantity of records that would exceed normal processing limits. The benefits of Batch Apex are that each batch class is run as its own transaction in the queue with its own governor execution limits. Plus, if one batch fails to process, it does not impact other batch transactions. 

Batch Apex Capabilities:

  • 200 SOQL query records per cycle
  • 50,000,000 SOQL queries can be retrieved
  • 12 MB heap size

Trailhead recommends that you only use Batch Apex if you have more than one batch of records. If you are not running more than one batch, Queueable Apex is the better option. 

Future Methods

Often used for a web service callout from an Apex trigger, Future Methods have the benefit of high governor and execution limits without blocking users from performing other operations. Future Methods are the best option when you need to isolate DML operations on different sObject types to prevent the mixed DML error. 

It’s important to note that Future Methods can’t take standard or custom objects as arguments because the sObject might change between the time you call the method and the time it executes. This may cause the future method to overwrite old sObject values. However, some developers have been using this workaround.

It is best practice to avoid adding large numbers of Future Methods to the asynchronous queue. When more than 2,000 unprocessed requests from a single organization are in the queue, any additional requests from the same organization will be delayed until the queue handles requests from other organizations.

Image depicting a monthly calendar

How Do You Schedule an Apex Job?

To schedule an Apex job other than Queueable, you first need to implement the Schedulable interface for the particular Apex class. Then, you have two options: one, create the schedule using the Schedule Apex page in the user interface or two, schedule the Apex job through the developer console using system.schedule method.

Option 1: Using the Schedule Apex Page 

  • From Setup, enter Apex Classes in the Quick Find box, select Apex Classes, and then click Schedule Apex.
  • Select the class that you want to schedule.
  • Determine how often the Apex class is to run, weekly, monthly, etc.
  • Enter the start and end dates for the Apex scheduled class. Note: if you specify a single day, the job only runs once.
  • Enter a preferred start time. The exact time the job starts will depend on service availability.
  • Click Save.

Option 2: system.schedule 

The system.schedule() approach takes three parameters:

  1. Name of the job.
  2. An expression that is used to represent the time and date of the operation.
  3. The object of the class which you want to execute.

Example:

MySchedulableClass sched = new MySchedulableClass();

String cronExp = ‘0 30 8 1 * * *’;  // 8:30am every 1st day of the month 

String jobID = System.schedule(‘Something descriptive’, cronExp, sched);

Regardless of the scheduling approach you choose, once the job is complete, you can see further details on the Apex Jobs page. For example, you can check whether it passed or failed, how long it took to process, or the number of records processed.

Keep in mind that you can only have 100 scheduled Apex jobs at one time. In addition, the maximum number of scheduled Apex executions in a 24-hour period is 250,000 or the number of user licenses in your organization multiplied by 200, whichever is greater.

It’s also important to note that asynchronous processing has lower priority than real-time interaction. To protect itself and create a fair environment for everyone, the queuing framework monitors resources like server memory and CPU usage and will limit asynchronous processing when thresholds are exceeded. 

Basically, there’s no guarantee on processing time.

Turning a No into a Yes

When basic Salesforce automation tools fall short, Apex code helps organizations manage complex business functionalities. All four scheduled apex functions are simply ways to run a piece of apex code at a particular time within a period of time. While Queueable Apex cannot be scheduled through the user interface, creative developers can find plenty of workarounds to solve this common dilemma.

Christine Maxey
Director of Revenue Operations at LeanData

Christine Maxey is the Director of Revenue Operations at LeanData, where she displays daily her passion for customer advocacy and obsession with customer happiness. Connect with Christine on LinkedIn.