Skip to main content

Asynchronous Apex

Explore practical guides and implementation patterns for this topic.

Posts

Apex Development

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
Apex Development

Salesforce Sync vs. Async Apex: How to Choose the Right Path

💬 In plain words: Synchronous (Sync) means the user waits for the process to finish, and you are constrained by tight platform limits. Asynchronous (Async) means the work happens later in the background, giving you much higher limits. Move work to async when it is slow (like API callouts or heavy calculations) or involves massive amounts of data. However, remember that async means "eventually." Do not use it if the user needs to see the result immediately on their screen. 📌 Real-Life Example: A user clicks "Save" on an order. You need to update the Salesforce record NOW (sync). But do you also need to notify your SAP system and recalculate the customer's yearly totals instantly? No. The user shouldn't have to stare at a loading spinner waiting for SAP to respond. Publish an event and queue the recalculation in the background. Think of sync as the fast lane for the user, and async as the freight lane for the heavy lifting. ...
Read article
Topic Apex
Apex Development

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.
Read article
Topic Apex
Apex Development

Metadata-Driven Record Deletion With Batch Apex

In plain words: A Metadata-Driven Deletion Batch is a reusable Batch Apex job that purges outdated records based on configuration rules stored in Custom Metadata Types (CMDT) . Instead of editing and deploying code whenever you need to clean up an object, administrators simply update metadata fields (such as target object name, date range, or active status), and the batch automatically executes the correct SOQL query. Maintaining data hygiene and keeping storage consumption within platform limits often requires automated record cleanup. However, hardcoding object names, date filters, or WHERE clauses directly inside Apex classes creates ongoing maintenance debt. Every requirement change forces developers to edit code, write new unit tests, and coordinate production deployments. Coupling Batch Apex with Custom Metadata Types creates a declarative engine that lets teams manage data retention policies without touching code.
Read article
Topic Apex
Apex Development

Queueable Apex: Job Chaining and Callouts in Practice

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.
Read article
Topic Integration & APIs
Salesforce Integration & APIs

HTTP Callouts in Batch Apex: AllowsCallouts and Limits

In plain words: Batch Apex with HTTP Callouts allows Salesforce to sync thousands of records with external systems (such as ERPs, payment gateways, or external databases) in manageable chunks. By adding the marker interface Database.AllowsCallouts to your batch class and capping your batch execution size to 100 records or fewer, you can make external REST/SOAP requests across millions of records without hitting governor limits. Synchronous transactions in Salesforce enforce strict timeout and callout limits, making it impossible to synchronize large datasets directly from UI controllers or record-triggered flows. When enterprise systems require bulk synchronization, scheduled data enrichment, or high-volume status verification, Batch Apex provides the scalable, asynchronous backbone needed to process records in controlled chunks over time.
Read article