Skip to main content

Batch 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.
Read article
Salesforce Basics

The Ultimate Guide to Salesforce Batch Apex: Processing Millions of Records Safely

💬 In plain words: Batch Apex is the heavy-duty freight train for processing millions of Salesforce records. It grabs a massive query, chops it into smaller chunks (default is 200 records), and processes them piece by piece. Because each chunk gets a fresh set of governor limits, you can safely update massive datasets. When volume is the problem, Batch Apex is the solution. 📌 The 4 Million Customer Example: Imagine you need to recalculate loyalty points for 4 million customers. Batch Apex queries them all at once (using a QueryLocator to stream up to 50 million rows safely) and processes them 200 at a time. That creates 20,000 mini-transactions, each with its own limits. If chunk #47 encounters an error and fails, the other 19,999 chunks will still commit successfully. 🎬 Real-Life Application: 3 Million Rows Before Breakfast A sudden price change means you need to recalculate the fuel surcharge on 3 million Delivery records overnight. ...
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 Batch Apex Best Practices Bulk Deletion Custom Metadata Types Data Archival Dynamic SOQL Salesforce Development
Batch Apex Best Practices Bulk Deletion Custom Metadata Types Data Archival Dynamic SOQL Salesforce Development

Metadata-Driven Record Deletion in Salesforce: Scalable Batch Apex Guide

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