Hey Learners! With the new Spring ’24 release, we have received a great add-on to the queueable jobs. Now we can set the maximum depth of chained queueable jobs. The "Configure Stack Depth of Chained Queueable Jobs" feature is now available in Salesforce for Developer and Trial Edition org. Users can set a custom maximum stack depth for Queueable jobs, surpassing the default limit of five. This feature acts as a safety measure, preventing runaway recursive jobs and enhancing the management of the daily async Apex limit. It applies to Lightning Experience and Salesforce Classic in Enterprise, Performance, Unlimited, and Developer editions.
Salesforce introduces a new System.enqueueJob() overload, allowing customization of maximum stack depth and minimum queue delay with the optional AsyncOptions parameter. Testing chained Queueable jobs is supported, but developers must be mindful of Apex governor limits. The new System.AsyncInfo class facilitates retrieving current and maximum stack depths, as well as minimum queueable delay information, enhancing control over asynchronous processes.
AsyncOptions Class
This class contains maximum stack depths for queueable transactions and the minimum queueable delay in minutes. It is passed as a parameter to the System.enqueueJob() method to define a unique queueable job signature, the maximum stack depth for queueable transactions, and the minimum queueable delay in minutes.
AsyncOptions Properties
The following are properties for AsyncOptions.
● DuplicateSignature
■ A unique signature for a Queueable job.
■ It serves as a unique identifier for a queueable job. In job queue systems, particularly those that may execute the same job multiple times, a unique signature helps in identifying and preventing the duplication of job instances in the queue.
■ Signature
● public System.QueueableDuplicateSignature
DuplicateSignature {get; set;}
● MaximumQueueableStackDepth
■ Maximum stack depth for queueable transactions.
■ Signature
● public Integer MaximumQueueableStackDepth {get; set;}
● MinimumQueueableDelayInMinutes
■ Minimum queueable delay for queueable transactions.
■ Signature
● public Integer MinimumQueueableDelayInMinutes {get; set;}
AsyncInfo class
This class provides methods to get the current stack depth, maximum stack depth, and the minimum queueable delay for Queueable transactions, and to determine if the maximum stack depth is set.
● Namespace
○ System
AsyncInfo Methods
The following are methods for AsyncInfo.
○ getCurrentQueueableStackDepth()
■ Get the current queueable stack depth for queueable transactions.
■ Signature
● public static Integer getCurrentQueueableStackDepth()
○ getMaximumQueueableStackDepth()
■ Get the maximum queueable stack depth for queueable transactions.
■ Signature
● public static Integer getMaximumQueueableStackDepth()
○ getMinimumQueueableDelayInMinutes()
■ Get the minimum queueable delay for queueable transactions (in minutes).
■ Signature
● public static Integer getMinimumQueueableDelayInMinutes()
○ hasMaxStackDepth()
■ Determine if the maximum stack depth is set for your queueable requests.
■ Signature
● public static Boolean hasMaxStackDepth()
Let’s see the feature with the help of an example class which will show how maximum depth functionality is working.
AccountDomain.cls
public class AccountDomain {
public static Account createAndInsertAccount(String name, String phone){
Account accounts = new Account(name = name, phone = phone);
insert accounts;
return accounts;
}
}
CreateAccount.cls
public class CreateAccount implements Queueable {
public static void callQueueable(Integer maxDepth) {
AsyncOptions asyncOptions = new AsyncOptions();
asyncOptions.MaximumQueueableStackDepth = maxDepth;
System.enqueueJob(new CreateAccount(), asyncOptions);
}
public void execute(System.QueueableContext context) {
Account account = AccountDomain.createAndInsertAccount(
'Avenoir Technologies Pvt. Ltd.',
'9987463210'
);
if (
System.AsyncInfo.hasMaxStackDepth() &&
AsyncInfo.getCurrentQueueableStackDepth() <
AsyncInfo.getMaximumQueueableStackDepth()
) {
System.enqueueJob(new CreateContact(account));
}
}
}
ContactDomain.cls
public with sharing class ContactDomain {
public static Contact createAndInsertContact(Account account) {
Contact contact = new Contact(
Lastname = 'Sharma',
AccountId = account.Id
);
insert contact;
return contact;
}
}
CreateContact.cls
public class CreateContact implements Queueable {
Account account;
public CreateContact(Account account) {
this.account = account;
}
public void execute(System.QueueableContext context) {
Contact contact = ContactDomain.createAndInsertContact(this.account);
if (
System.AsyncInfo.hasMaxStackDepth() &&
AsyncInfo.getCurrentQueueableStackDepth() <
AsyncInfo.getMaximumQueueableStackDepth()
) {
System.enqueueJob(new CreateOpportunity(this.account));
}
}
}
OpportunityDomain.cls
public with sharing class OpportunityDomain {
public static Opportunity createAndInsertOpportunity(Account account) {
Opportunity opportunity = new Opportunity(
Name = 'Test Opportunity',
StageName = 'Qualification',
CloseDate = System.today(),
AccountId = account.Id
);
insert opportunity;
return opportunity;
}
}
CreateOpportunity.cls
public class CreateOpportunity implements Queueable {
Account account;
public CreateOpportunity(Account account) {
this.account = account;
}
public void execute(System.QueueableContext context) {
Opportunity opportunity = OpportunityDomain.createAndInsertOpportunity(this.account);
}
}
On calling callQueueable() of createAccount class by passing depth as 2 it will create Account and Contact
On calling callQueueable() of createAccount class by passing depth as 3 it will create Account, contact and opportunity
Conclusion
The Configure Stack Depth of Chained Queueable Jobs feature in Salesforce allows developers to set a maximum stack depth for Queueable jobs, preventing recursive jobs from exceeding limits. This provides better control and safety mechanisms for asynchronous Apex execution, improving reliability and minimizing the risk of hitting governor limits. The example code illustrates how to use this feature to manage stack depth effectively.’
If you'd like to see the code and resources used in this project, you can access the repository on GitHub.To access the AVENOIRBLOGS repository, click here. Feel free to explore the code and use it as a reference for your projects.
Thank You! You can leave a comment to help me understand how the blog helped you. If you need further assistance, please contact us. You can click "Reach Us" on the website and share the issue with me.
Reference
Blog Credit:
A. Agrawal
Salesforce Developer
Avenoir Technologies Pvt. Ltd.
Reach us: team@avenoir.ai
Are you in need of Salesforce Developers?
Reach Us Now!
Comments