Skip to main content

Asynchronous Apex

Explore practical guides and implementation patterns for this topic.

Posts

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
Apex & Dev

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 & 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 Apex & Dev
Apex & Dev

Salesforce Apex Flex Queue: Batch Job Management, Limits & Reordering Guide

In plain words: Think of the Apex Flex Queue as a digital waiting room for your batch jobs. Salesforce can run up to 5 batch jobs at the same time. The Flex Queue holds up to 100 additional batch jobs in a Holding status, automatically feeding them into execution as processing slots open up. Managing large-scale data processing in Salesforce requires balancing high data volumes with strict multi-tenant governor limits. While synchronous transactions must complete within milliseconds, Batch Apex handles millions of records asynchronously. The Apex Flex Queue is the core platform infrastructure that governs how these batch jobs are queued, prioritized, and executed without failing due to concurrency constraints. 1. How the Apex Flex Queue Operates Prior to the Flex Queue, submitting a batch job when all processing threads were full resulted in immediate job rejection. The Flex Queue eliminates this limitation by introducing the Holding status: Holdi...
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
Topic Asynchronous Apex Backend Development Batch Apex Governor Limits Salesforce Salesforce Developer
Asynchronous Apex Backend Development Batch Apex Governor Limits Salesforce Salesforce Developer

Salesforce Batch Apex Guide: How to Process Large Data Volumes

In plain words: Batch processing in Salesforce (known as Batch Apex) is a way to handle massive amounts of data by breaking it down into smaller, manageable pieces. If you try to update 50,000 records at once in a standard script, Salesforce will crash and throw a Governor Limit error. Batch Apex safely processes those records in background chunks (like 200 at a time) until the whole job is done. In enterprise Salesforce environments, dealing with large datasets is a daily reality. Whether you are running a nightly sync to update thousands of Accounts or purging old log files, standard synchronous Apex simply cannot handle the load. This is where Asynchronous Apex, specifically Batch Processing, steps in. Let's explore how Batch Apex works, why it is critical for performance, and how you can implement it from scratch. Why Do We Need Batch Apex? Salesforce operates in a multi-tenant cloud, meaning everyone shares the same server resources. To prevent on...
Read article
Topic Apex & Dev
Apex & Dev

Top 15 Salesforce Developer Interview Questions and Answers (Apex, Asynchronous Limits & Lightning)

Technical interviews for Salesforce developers evaluate not just your syntax knowledge, but how well you understand governor limits, asynchronous design patterns, and modern UI architectures. Here is a curated guide to 15 essential Salesforce interview questions with clear explanations and best-practice examples. In plain words: Salesforce technical interviews test your real-world problem solving: how you process high-volume data within execution limits, decouple integrations, and coordinate communication across UI components. 1. Asynchronous Apex & Batch Processing Q1: What are the batch size boundaries in Batch Apex? When invoking Database.executeBatch(instance, scopeSize) , the batch execution boundaries are: Default Batch Size: 200 records. Minimum Batch Size: 1 record. Maximum Batch Size: 2,000 records. Real-Life Scenario: If your batch's execute() method makes external callouts or executes heavy transactional...
Read article