How to Schedule Queueable Apex (2024) (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.

How to Schedule Queueable Apex (2024) (1)

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.

How to Schedule Queueable Apex (2024) (2)
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.

How to Schedule Queueable Apex (2024) (3)
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.

How to Schedule Queueable Apex (2024) (4)
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.

How to Schedule Queueable Apex (2024) (5)
Source: Brian Fear

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

Curious how LeanData eliminates the need for Apex code?

Watch a 100-Second Demo Here

Watch Demo


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.

How to Schedule Queueable Apex (2024) (6)

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
How to Schedule Queueable Apex (2024) (2024)

FAQs

Is it possible to schedule a queueable in Apex? ›

However, you can indirectly schedule a Queueable Apex job by invoking it from a scheduled Apex class or using a time-based workflow or process that triggers an Apex class, which then enqueues the Queueable job. This approach allows you to execute Queueable Apex at specified times or intervals.

What is the maximum number of Queueable jobs? ›

Queueable Apex Limits

You can add up to 50 jobs to the queue with System. enqueueJob in a single transaction. In asynchronous transactions (for example, from a batch Apex job), you can add only one job to the queue with System.

How many queueable jobs can be chained? ›

Queueable apex can be called from the Future and Batch class. In Queueable Apex, we can chain up to 50 jobs and in the developer edition, we can chain up to 5 jobs only whereas in Batch Apex 5 concurrent jobs are allowed to run at a time.

Can we call Queueable from Apex? ›

Firstly, to execute queueable apex we need to implement the interface Queueable. Also, the method which is implementing the Queueable interface needs to be declared as public or global. If you want to allow callout then we also need to implement Database.

Is Queueable apex synchronous or asynchronous? ›

Queueable Apex allows you to submit jobs for asynchronous processing similar to future methods with the following additional benefits: Non-primitive types: Your Queueable class can contain member variables of non-primitive data types, such as sObjects or custom Apex types.

What is the difference between Queueable and Schedulable apex? ›

Queueable Apex offers flexibility and job chaining. Future Methods are great for simple background tasks. Schedulable Apex is ideal for recurring jobs.

How do I fix too many queueable jobs added to the queue? ›

Implement Throttling Mechanisms: Implement mechanisms in your code to throttle the number of Queueable Apex jobs being added to the queue within a certain time period. You can use custom logic to control when and how many jobs are queued based on factors such as system load or available resources.

What is the advantage of queueable apex over future methods? ›

Future Vs queueable apex
FUTURE APEXQUEUEABLE APEX
we cannot call a future from another future or batch apex. The limit on future method for single apex invocation is 50.We can chain the Queueable jobs and the stack depth in developer org is 5 and in enterprise edition you can chain 50 jobs.
3 more rows

Can we call a queueable class from batch? ›

Queueable Apex can be used in place of future methods when you need to chain jobs (call another job from a job), and they can also be called from batch classes.

Can we call Queueable in future? ›

Queueable Apex can be called from the future and batch class.

Can queueable be called from trigger? ›

Queueable Apex is just another flavor of Async Programming implementation offered by the Salesforce Platform. In this article, we will explore how to call a Queueable Job from within the Trigger.

Can we make callouts from queueable apex? ›

Queueable apex is an asynchronous apex method. It's similar to the @future method. By using this queueable interface, we can process an Apex that runs for a long time (such as web service call outs and extensive database operations). Queueable apex is an asynchronous apex method.

Can we schedule apex class in Salesforce? ›

Once you've created and tested your Apex class, you can schedule it. This can be executed either through coding programmatically or via the Salesforce user interface. To schedule programmatically, you can use the System. schedule method.

Can you call a Queueable from a batch future method? ›

Queueable Apex can be used in place of future methods when you need to chain jobs (call another job from a job), and they can also be called from batch classes.

Can batch apex be scheduled? ›

You can call the System. scheduleBatch method to schedule a batch job to run one time at a specified time in the future. This method is available only for batch classes and doesn't require the implementation of the Schedulable interface. It's therefore easy to schedule a batch job for one execution.

Can we schedule a future method in Apex? ›

Cannot be Scheduled: A future method executes as soon as resources are available. So you can't schedule it to execute at a specific time. If you need execution at a specific time then you should use Apex Scheduler. No Monitoring: A future method doesn't have a job ID.

References

Top Articles
Latest Posts
Article information

Author: Gregorio Kreiger

Last Updated:

Views: 5662

Rating: 4.7 / 5 (57 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Gregorio Kreiger

Birthday: 1994-12-18

Address: 89212 Tracey Ramp, Sunside, MT 08453-0951

Phone: +9014805370218

Job: Customer Designer

Hobby: Mountain biking, Orienteering, Hiking, Sewing, Backpacking, Mushroom hunting, Backpacking

Introduction: My name is Gregorio Kreiger, I am a tender, brainy, enthusiastic, combative, agreeable, gentle, gentle person who loves writing and wants to share my knowledge and understanding with you.