Close Menu
SalesforceCodex
    Facebook X (Twitter) Instagram
    Trending
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    • Top 10 PMD Issues Salesforce Developers Should Focus on in Apex
    • How to Use Graph API for Outlook-Salesforce Connection
    • Enhancing Performance with File Compression in Apex
    Facebook X (Twitter) Instagram
    SalesforceCodex
    Subscribe
    Tuesday, May 20
    • Home
    • Architecture
    • Salesforce
      • News
      • Apex
      • Integration
      • Books Testimonial
    • Questions
    • Certification
      • How to Prepare for Salesforce Integration Architect Exam
      • Certification Coupons
    • Integration Posts
    • Downloads
    • About Us
      • Privacy Policy
    SalesforceCodex
    Home»Salesforce»Apex»How to Implement Dynamic Queueable Chaining in Salesforce Apex

    How to Implement Dynamic Queueable Chaining in Salesforce Apex

    Dhanik Lal SahniBy Dhanik Lal SahniApril 21, 2025Updated:April 24, 2025No Comments5 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    Queueable Chaining in Salesforce Apex
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Queueable jobs are excellent ways to run apex classes asynchronously. This is used to overcome govern limits imposed by Salesforce at runtime. We need to run multiple queueable jobs in order in a few business scenarios. In our last post (How to Implement Basic Queueable Chaining in Salesforce Apex), we covered static ways to run multiple queueable jobs in order. This post will implement dynamic queueable chaining in Salesforce Apex.

    Implementing dynamic Queueable chaining in Salesforce Apex allows us to flexibly execute multiple Queueable jobs in sequence without hardcoding the next job. This is useful for modularizing long-running operations or when you want to execute a series of tasks dynamically based on runtime decisions.

    Why use dynamic Queueable chaining?

    • To avoid hitting governor limits in a single transaction.
    • To modularize large processes.
    • To dynamically decide the next step based on logic or input.

    Implement Dynamic Queueable chaining

    In static queueable chaining, we implement queueable chaining by running the next queueable job after finishing the current running job. We hardcode the next job in this logic. It is called strong coupling. Refer to the post How to Implement Basic Queueable Chaining in Salesforce Apex to see this kind of implementation.

    In dynamic queueable chaining, we can pass the next queueable job based on logic or based on some configuration at runtime. No need to hardcode the next job in the code. We can inject the next job using a constructor or a method injection. This is called loose coupling.

    Use Case for Implementing Dynamic Queueable Chaining

    Medstore, a pharma company, sells medical products online. Order processing needs to be done within the Salesforce system. It should validate orders, process payments, generate invoices, and then update inventory.

    It is the same use case mentioned in the previous post, How to Implement Basic Queueable Chaining in Salesforce Apex

    Implementation

    Create ChainableQueueable interface with a method setNextJob. This will be implemented by all queueable jobs to set the next job.

    ValidationQueueable

    This queueable apex will validate orders and customer information for order processing. Once the order queueable logic is finished, it will queue the next job, passed using the setNextJob method.

    PaymentQueueable

    PaymentQueueable class will handle payment-related logic for order processing. It might be a new payment system addition or an existing wallet/card payment; it will handle all the business logic. Once all payment information is finished, it will queue the next job, passed using the setNextJob method.

    InvoiceQueueable

    InvoiceQueueable will handle business logic related to invoicing. All document processing can be handled in this queueable. Once we finish invoicing logic, it will queue the next job, passed using the setNextJob method.

    InventoryQueueable

    InventoryQueueable apex jobs will handle inventory-related logic, like updating product inventory in the warehouse. We can implement logic like, if inventory goes beyond the minimum threshold, send an email to the warehouse manager about the inventory. It will queue the next job, passed using the setNextJob method.

    OrderRequest wrapper class has parameters that will be used to pass information to all queueable classes. These parameters will be passed from the first queueable class (ValidationQueueable). Wrapper class properties can be updated in any of the queueable based on business logic.

    public class OrderRequest {
    public string customerId {get;set;}
    public string paymentId {get;set;}
    public string orderId {get;set;}
    }

    OrderService class has logic to queue a job. We can implement it based on the required business requirement. For this post, I have just set job enque logic, but you can implement chaining logic in the custom metadata type as well. This will reduce code changes and increase maintainability.

    Benefits of this solution

    1. Flexibility & Configurability – We don’t need to hardcode queueable class names. We can store the class sequence in Custom Metadata or Custom Settings and modify it without changing code.
    2. Reusability: Same chaining framework can execute any combination of queueables. Each queueable does one thing — promoting Single responsibility principle.
    3. Scalability: Easily chain multiple jobs in sequence without hitting governor limits. Each job runs in its context, so you can process large data sets more reliably.
    4. Improved Maintainability: Future enhancements become easier — just add new queueables and register them in metadata. No need to rewrite or redeploy the core logic.

    Disadvantages of this solution

    • For small use cases, dynamic chaining may be over-engineered. Static chaining is simpler and easier to read when the job flow is fixed.
    • Unit tests need to account for dynamic instantiation and multiple job chaining.
    • Salesforce has a limit of 50 queueable jobs per transaction. If we enqueue too many in a single chain, it will throw a governor limit error.

    Similar Posts

    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • Avoid Batch Apex and Use Queueable Class
    • Transaction Finalizers for Salesforce Queueable Job
    • Enhancing Performance with File Compression in Apex
    • How to Correctly Publish Platform Event Using Salesforce Apex
    • How to Manage Technical Debt in Salesforce
    • How to Elevate Your Career to Salesforce Architect
    • Handle Heap Size for Apex Code Optimization
    • Implementing Apex Cursors for Optimal Resource Management in Salesforce
    • Build Scalable Solutions with Salesforce
    • Dynamic Code Execution using Callable Interface
    • Optimizing Salesforce Apex Code
    • Revisit Asynchronous Apex : Type and Usage
    • Salesforce Interview Question for Asynchronous Apex
    • Top 20 Salesforce Developer Interview Questions
    • Queueable Vs Batch Apex In Salesforce

    apex architecture dynamic architecture Queueable queueable apex salesforce salesforce apex
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleHow to Implement Basic Queueable Chaining in Salesforce Apex
    Next Article Unlock the Power of Vibe Coding in Salesforce
    Dhanik Lal Sahni
    • Website
    • Facebook
    • X (Twitter)

    With over 18 years of experience in web-based application development, I specialize in Salesforce technology and its ecosystem. My journey has equipped me with expertise in a diverse range of technologies including .NET, .NET Core, MS Dynamics CRM, Azure, Oracle, and SQL Server. I am dedicated to staying at the forefront of technological advancements and continuously researching new developments in the Salesforce realm. My focus remains on leveraging technology to create innovative solutions that drive business success.

    Related Posts

    By Dhanik Lal Sahni6 Mins Read

    Top 10 Salesforce Flow Features of Salesforce Summer ’25

    May 11, 2025
    By Dhanik Lal Sahni6 Mins Read

    Unlock the Power of Vibe Coding in Salesforce

    April 30, 2025
    By Dhanik Lal Sahni5 Mins Read

    How to Implement Basic Queueable Chaining in Salesforce Apex

    March 31, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (110) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) code review tools (3) custom metadata types (5) design principle (9) einstein (3) flow (15) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (29) Lightning web component (61) lwc (50) named credential (8) news (4) optimize apex (3) optimize apex code (4) Permission set (4) Queueable (9) rest api (23) S3 Server (4) salesforce (140) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (110) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) code review tools (3) custom metadata types (5) design principle (9) einstein (3) flow (15) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (29) Lightning web component (61) lwc (50) named credential (8) news (4) optimize apex (3) optimize apex code (4) Permission set (4) Queueable (9) rest api (23) S3 Server (4) salesforce (140) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Facebook X (Twitter) Instagram Pinterest YouTube Tumblr LinkedIn Reddit Telegram
    © 2025 SalesforceCodex.com. Designed by Vagmine Cloud Solution.

    Type above and press Enter to search. Press Esc to cancel.

    Ad Blocker Enabled!
    Ad Blocker Enabled!
    Our website is made possible by displaying online advertisements to our visitors. Please support us by disabling your Ad Blocker.