Blog

How to Schedule Queueable Apex (2024)

Queueable Apex is one of four asynchronous Apex interface classes.

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 classes have 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
  3. Chain jobs in a sequence

Unlike other classes, Queueable Apex classes is an interface that allows developers to add jobs to the queue and monitor them. Each queued job runs when an Asynchronous Apex Job slot becomes available.


Benefits of an Interface

One benefit of using an interface is that some governor limits are higher than 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.

If you are chaining Queueable jobs (i.e., a Queueable job enqueues another Queueable job during its execution), Salesforce allows only one additional job to be enqueued in this way. This is a safeguard to prevent excessive chaining, which could potentially lead to resource hogging and other issues. If you try to enqueue a second Queueable job within an already executing Queueable job, you would hit this limit.


Can You 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.

Salesforce developer coding on a large monitor



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 on 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.

Ready to unlock new revenue possibilities with LeanData?

Meet with one of our experts today
Let’s Chat


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, weekly, or monthly 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.

A modern office full of people sitting at desks while coding software programs



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 

  • (Salesforce Classic) From Setup, enter Apex Classes in the Quick Find box, select Apex Classes, and then click Schedule Apex.
  • (Salesforce Lightning) Navigate to Setup, select Custom Code, select Apex Classes, then click on the “Schedule Apex” button
  • Select the class that you want to schedule.
  • Determine how often the Apex class is to run: daily, 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 the availability of an Asynchronous Apex Job slot.
  • 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 completed 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 for all 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. While there are four Apex interfaces, only one of them allows you to run at a particular period of time. In addition, while Queueable Apex cannot be scheduled through the user interface, creative developers can find plenty of workarounds to solve this common dilemma.

Tags
  • information technology
  • IT
  • Salesforce
  • Salesforce Queueable Apex
  • SFDC
  • SFDC Admin