Skip to main content

Queueable Apex

Explore practical guides and implementation patterns for this topic.

Posts

Salesforce Basics

Salesforce Async Apex Explained: The "Who Can Call Whom" Matrix

💬 In plain words: Navigating asynchronous Apex is all about knowing who is allowed to call whom. A Queueable can chain itself, and a Batch can start a Queueable . However, a @future method is a dead end—it cannot call another @future , and a Batch cannot call a @future . Memorizing this simple matrix eliminates an entire category of runtime crashes. Key Points at a Glance The Future is a Dead End: You cannot call a @future method from any other async context (Batch, Queueable, or another Future). Queueable is the Swiss Army Knife: You can enqueue a Queueable from a Trigger, a Batch execute() or finish() , and even chain it to another Queueable. Batch Chaining: You can legally chain a new Batch job, but only from the finish() method of the current Batch. Callout Rule: Callouts require the correct marker ( callout=true or Database.AllowsCallouts ) and can never occur after a DML operation in the same transaction. 📌 Real...
Read article
Salesforce Basics

Master Salesforce Queueable Apex: Chaining, Finalizers, and Cursors

💬 In plain words: Queueable Apex is Salesforce's modern asynchronous worker. Unlike older methods, it accepts complex objects as parameters and returns a trackable Job ID. Best of all, it allows for chaining—one job can trigger the next, passing the baton like a relay race. By attaching a Transaction Finalizer , you build a safety net that guarantees specific code will run (for logging or retrying) even if the main job crashes. 📌 The Order Fulfillment Example: Imagine an order ships. Job 1 calls the courier API. Once complete, it enqueues Job 2 to update the inventory system. Job 2 then enqueues Job 3 to email the invoice. This is an organized relay race where every runner gets a fresh set of governor limits. If Job 2 crashes due to a limit exception, its Transaction Finalizer catches the failure, logs it, and re-queues the job to try again. 🎬 Real-Life Application: The Relay That Never Drops the Baton A company needs to process b...
Read article
Apex & Dev

Salesforce @future Methods vs. Queueable: The Modern Guide

💬 In plain words: The @future annotation is the original "fire-and-forget" asynchronous tool in Salesforce. You tag a static method, and the platform runs it later. However, its strict limitations—only accepting primitive data types, no ability to chain jobs, and no traceable Job ID—are the exact reasons Queueable Apex was created to replace it. Today, @future is mainly used as a quick escape hatch for Mixed-DML errors. 📌 The Problem: In legacy code, a trigger might call @future(callout=true) to push data to an external ERP. It works fine until a developer tries to trigger that same logic from a Batch Apex job, resulting in the dreaded error: "future method cannot be called from a future or batch method." The Fix: The modern solution is to rewrite that logic as a Queueable class, leaving @future purely for resolving quick Setup vs. Non-Setup object conflicts (Mixed-DML). 🎬 Real-Life Example: The Stale Snaps...
Read article
Topic Apex & Dev
Apex & Dev

How to Fix "You Have Uncommitted Work Pending" Error in Salesforce

💬 In plain words: The error System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out occurs when your code changes the database (like using an insert or update statement) and then tries to send an HTTP callout to an external server in the exact same synchronous process. Salesforce actively blocks this to prevent database locks from hanging open while waiting for external servers to reply. When developing custom backend integrations in Salesforce, encountering the "uncommitted work pending" error is almost a rite of passage. Understanding why this strict transaction limit exists—and how to architect your Apex to respect it—is the key to building resilient, enterprise-grade system integrations. 📌 Key Points The Root Cause: Mixing Data Manipulation Language (DML) and HTTP Callouts in the wrong order. The Golden Rule: You can perform a Callout before DML, but you can never perform DML befo...
Read article
Topic Bulkification Governor Limits LimitException Queueable Apex Salesforce Apex Salesforce Architecture SOQL Optimization
Bulkification Governor Limits LimitException Queueable Apex Salesforce Apex Salesforce Architecture SOQL Optimization

Mastering Salesforce Apex Governor Limits: Common Exceptions & Mitigation Strategies

In plain words: Governor Limits are Salesforce's traffic rules for multi-tenant architecture. Because many companies share the same underlying cloud infrastructure, Salesforce enforces strict transaction caps (like memory, queries, and CPU time) so one piece of inefficient code cannot slow down or crash the shared platform. Salesforce executes Apex code in a multi-tenant cloud environment. To guarantee high performance, platform stability, and fair resource sharing, the runtime engine enforces strict per-transaction limits known as Governor Limits . When code exceeds these boundaries, Salesforce throws a fatal runtime LimitException that immediately halts execution. Writing resilient Apex requires a clear understanding of these boundaries and defensive design patterns. 1. Understanding Core Governor Limits Governor limits monitor resources across synchronous and asynchronous transactions. Understanding these thresholds is essential for designing high-thro...
Read article
Topic Apex Triggers Asynchronous Apex Database.AllowsCallouts Governor Limits Job Chaining Queueable Apex Salesforce Development
Apex Triggers Asynchronous Apex Database.AllowsCallouts Governor Limits Job Chaining Queueable Apex Salesforce Development

Mastering Queueable Apex in Salesforce: Job Chaining, Callouts & Best Practices

In plain words: Queueable Apex is an asynchronous execution framework in Salesforce that lets you run resource-heavy or long-running tasks in the background. Unlike legacy @future methods, Queueable Apex allows you to pass complex data types (such as sObjects and collections), returns a trackable AsyncApexJob ID immediately, and supports sequential job chaining to execute multi-step background processes. Synchronous transactions in Salesforce have strict governor limits—such as CPU timeouts and heap size caps—to ensure platform resources remain shared fairly across all tenants. When your business logic requires heavy data processing, complex calculations, external REST callouts, or operations that risk slowing down the user experience, offloading the work asynchronously is critical. Queueable Apex combines the simplicity of future methods with the power of batch classes. 1. Why Choose Queueable Apex? Salesforce provides several asynchronous tools, but Queuea...
Read article
Apex & Dev

How to Fix the Mixed DML Operation Error in Salesforce (Ultimate Guide)

In plain words: Salesforce divides its database into two groups: setup objects (which control system access, like Users and Permission Sets) and non-setup objects (which hold your everyday business data, like Accounts and Cases). You cannot modify both types in a single transaction. Doing so triggers a MIXED_DML_OPERATION error. To fix it, you must separate them using asynchronous Apex (like Queueable or @future), Flow Async Paths, or System.runAs() in test classes. Imagine you write a simple trigger: when a new partner Account is created, automatically assign the Account Manager a specific Permission Set. It seems perfectly logical, yet it fails instantly upon testing. This happens because you've hit one of Salesforce's most fundamental data security rules: you are mixing setup and non-setup data in a single breath. In this guide, we'll break down exactly why this rule exists, the latest ways to properly design around it using modern Apex and Fl...
Read article
Topic Apex & Dev
Apex & Dev

Top 10 Salesforce Interview Questions and Answers (Apex, Lightning & Integration)

Preparing for a Salesforce developer or technical architect interview requires mastering both core declarative mechanics and programmatic architecture. Here is a breakdown of 10 essential interview questions covering data modeling, asynchronous Apex, event-driven architecture, and UI frameworks. In plain words: Salesforce technical interviews test your understanding of governor limits, secure integrations, event handling, and data relationships—focusing on when to use declarative configuration versus programmatic code. 1. Data Architecture: Junction Objects Question: What is a Junction Object in Salesforce? A Junction Object is a custom object with two Master-Detail relationship fields. It creates a many-to-many relationship between two parent objects. Real-Life Example: In a hiring app, one Candidate can apply for multiple Positions , and one Position receives applications from multiple Candidates . The custom object JobApplication__c acts as...
Read article