Queueable Apex
🔹 What is Queueable Apex?
Queueable Apex is an asynchronous Apex feature that allows you to run
background jobs with more flexibility and power than @future methods.
🔹 Why use Queueable Apex?
You use Queueable Apex when:
You need to perform long-running or complex operations in the background.
You need to pass complex data like custom objects or collections.
You want to chain multiple jobs one after another.
You want more control and flexibility than @future .
🔹 Key Features of Queueable Apex:
Feature Description
Implements Queueable Class must implement Queueable interface.
Has execute method Logic goes inside execute(QueueableContext context) .
Supports Complex You can pass custom objects and collections to the
Parameters constructor.
Supports Chaining Can enqueue another job inside execute() for chaining.
Own Governor Limits Runs in its own context with separate limits.
Can Monitor in UI Jobs show up in Apex Jobs in Setup.
Queueable Apex 1
Simple Example:
public class AccountQueueable implements Queueable {
public void execute(QueueableContext context) {
List<Account> accs = [SELECT Id, Name FROM Account WHERE Rating = 'Ho
t'];
for(Account a : accs) {
[Link] = 'Warm';
}
update accs;
}
}
Run from Apex:
[Link](new AccountQueueable());
Need for Queueable Apex:
Limitation in Future method
In Future, one future method can not be called from another future method
(Jobs can not be chained).
Future method can not be schedulable(Since it's a method)
Can not be called from execute in batch apex
Does not support SObject as parameter
Queueable Apex
Helps to implement Asynchronous Apex in Object Oriented Programming
One Queueable Apex can be called from another Queueable Apex (Jobs can
be chained)
Supports SObject as parameter
Can be monitored using job id
Queueable Apex 2
Interface Queueable used to implement Queueable Apex
Queueable Interface
Interface Queueable used to implement Queueable Apex
This interface enables you to add jobs to the queue and monitor them
Usage
Long running database operations
Long running complex business process
Web service callout
Queueable - Interface which has a execute method
- This is the execute method which has Params and returns Job
QueueableContext
Id, Queueable will give job Id & when a Queueable is executed it returns a Job
Id
Chaining Jobs - You can chain another Queueable job inside execute() , allowing
sequential processing.
🔹 Queueable vs Future – Why Prefer Queueable?
Feature @future Queueable
Complex Parameters ❌ No ✅ Yes
Chaining ❌ No ✅ Yes
Job Monitoring ❌ Limited ✅ Visible in Apex Jobs
Governor Limits ✅ Own limits ✅ Own limits
Summary (One-liner):
Queueable Apex is a powerful way to run background logic
with support for complex data, job chaining, and monitoring—a
better version of @future.
Queueable Apex 3
Chaining Jobs in Queueable Apex
Helps to call queuable apex in a sequential way in Asynchronous Apex
Each Queueable call will run its own thread
We can not make more than one queueable call from execute method
User Story 1.0
ABC Industry wants to implement functionality so that all the account records
should be updated with discount percentage based on rating given by the user
public class QueueableDemo implements Queueable{
String strRating;
public QueueableDemo(String rating)
{
strRating=rating;
}
public void execute(QueueableContext ctx)
{
List<Account> lstAccount=[Select Id, Name, Discount__c, [Link]
From Account
Where rating=:strRating Limit 10000];
for(Account record : lstAccount)
{ //custom Label
record.Discount__c = [Link]([Link]);
Queueable Apex 4
}
update lstAccount;
}
}
//[Link](new QueueableApexDemo('Hot'));
User Story 1.1
ABC Industry wants to send email to all the account record owners once discount
was updated
Business wants to commit the account record first and then send email when
resources available
public class QueueableDemo implements Queueable{
string strRating ;
integer batchSize;
string lastRecordId;
public QueueableDemo(string Rating,integer batchSize,string lastRecordId)
{
strRating =Rating;
[Link]=batchSize;
[Link] = lastRecordId;
}
public void execute(QueueableContext xyz)
{
list<Account> acclist = [Select id,rating from Account
where rating =: strRating and id >: lastRecordId limit : batchSize
];
[Link](acclist);
for(Account accrecord : acclist)
{
accrecord.Discount_Amount__c = 20;
Queueable Apex 5
}
update acclist;
lastRecordId = acclist[[Link]()-1].id;
if([Link]()==batchSize)
[Link](new QueueableDemo(strRating,batchSize,lastRecordId));
[Link](new EmailQueueableHelper(acclist));
}
}
public class EmailQueueableHelper implements Queueable {
list<Account> acclist ;
public EmailQueueableHelper(list<Account> acc)
{
acclist = acc;
}
public void execute(QueueableContext xyz)
{
list<[Link]> emailist
= new list<[Link]>();
String Subject = 'Account Discount Update';
String Body = 'Account Discount Update';
for(Account accRecord : acclist)
{
[Link] mail = new [Link]();
string toAddress = [Link];
list<String> toAddresses = new list<String>{toAddress};
[Link](toAddresses);
[Link](Subject);
[Link](Body);
[Link](mail);
Queueable Apex 6
}
[Link](emailist);
}
}
//[Link](new QueueableApexDemo('Hot'));
What is Chaining Jobs?
Helps to call queueable apex in a sequential way in Asynchronous Apex, We can
not make more than one queueable call from execute method
Stack Depth
What is stack depth?
Stack depth means, how many layers of recursion or Queueable Apex
invocations(Enqueue Job) you can have before hitting limits.
Queueable Apex 7
Stack Depth Limit
From Winter 24 salesforce made stack depth as configurable one overriding the
default limit of five
[Link] class
Helps to determine the current and maximum stack depths
Methods available
hasMaxStackDepth()
getMaximumQueueableStackDepth()
getCurrentQueueableStackDepth()
How to use
AsyncOptions asyncOptions = new AsyncOptions();
[Link] = 10;
[Link]( new QueueableStackDepthDemo(), asyncOptions);
Limitation
Solution:-
Introducing recursive queueable apex call with maximum stack depth tracking
Recursive Queueable Apex
you can break up large data processing tasks into manageable chunks,
ensuring that they complete successfully while staying within Salesforce's
Queueable Apex 8
governor limits.
Maximum Queueable Jobs Allowed/Day
For Developer and Sandboxes:
50,000 Queueable jobs per 24-hour period.
For Production and Full Sandboxes:
100,000 Queueable jobs per 24-hour period.
Test Class for Queueable Apex
Test Class for Queueable Apex (Without Chaining of Jobs)
Test Classes are important to deploy the code change to production
Its mandatory to have 75% code coverage(Always aim for 90%)
In test class Asynchronous calls are executed Synchronous only
Use [Link]() and [Link]() method and call the Queueable Apex
Immediately after [Link]() it Will collect all the Asynchronous calls and after
[Link]() it will run synchronously
Any assert related to Queueable apex call should me made after the
Teststoptest() method
Test Class - Chaining Queueable Apex
Test class will fail if you have chaining of jobs in the queueable apex
Use [Link]() in your queueable apex class to stop chaining when test
executed
Cover the chained queueable apex class as normal apex class
@isTest
public class QueueableDemoTest {
@TestSetup
Queueable Apex 9
public static void doSetupData()
{
list<Account> acclist = new list<Account>();
for(integer count=1;count<=15;count++ )
{
[Link](new Account(Name = 'Test '+count,Rating = 'Hot'));
}
insert acclist;
}
@isTest
public static void doQueueable()
{
[Link]();
AsyncOptions obj = new AsyncOptions();
[Link] =10;
[Link](new QueueableDemo('Hot',10,'0'),obj);
[Link]();
list<Account> aclist = [Select id,Rating,Discount_Amount__c,[Link]
from Account where Rating = 'Hot' and Discount_Amount__c = 20
];
[Link](15,[Link](),'Expected out come is 15');
}
}
💡 Test class will run a recurring queueable implementation
Test class will not run when two different job are chained.
Queueable Apex 10
Notes:-
Queueable Jobs per Transaction:
A single transaction can enqueue up to 50 Queueable Apex jobs.
Concurrent Queueable Jobs:
You can have a maximum of 5 concurrently running Queueable jobs at
any given time. This limit applies to the number of jobs executing in
parallel.
Queueable Jobs in 24 Hours:
Your organization is limited to a total of 250,000 Queueable jobs per 24-
hour period.
Queueable Apex 11