Skip to main content

Apex Triggers

Explore practical guides and implementation patterns for this topic.

Posts

Salesforce Basics

Salesforce Trigger Frameworks: Bulkification & Reentrancy Explained

⚡ 1-Minute Summary: Governor limits act as a transaction budget shared across your code, flows, and packages. Strictly enforce one trigger per object . Keep logic out of the trigger itself and build a bypass switch on day one. Use Queueable as your default asynchronous tool. Avoid writing new @future methods.
Read article
Salesforce Platform & Administration

The Ultimate Guide to Salesforce Flows: Before-Save vs. After-Save (10x Faster Updates)

💬 In plain words: Before-save updates in Salesforce are entirely free from a performance standpoint. Because the record is already open in memory, you are simply writing on it before it gets saved. After-save updates, however, force the system to re-open the record and run the entire save process all over again, essentially doubling your resource cost. The golden rule is simple: if you are changing the exact same record that triggered the flow, do it before the save. If you are updating related records, do it after the save.
Read article
Apex Best Practices Apex Triggers Data Integrity Date Overlap Developer Guide Salesforce

How to Prevent Overlapping Date Ranges in Salesforce with Apex

In plain words: An overlapping date trigger acts as a bouncer for your database. It checks the calendar before a new record is saved to ensure you don't double-book a room, assign conflicting tax rates, or schedule overlapping employee shifts. If a conflict exists, it blocks the save and shows the user an error message. Maintaining pristine data quality in Salesforce often requires business rules that go beyond standard matching rules. When dealing with time-sensitive records—like subscription tiers, tax rates, or resource bookings—you must guarantee that no two records for the same category overlap in time. Because standard duplicate rules only look for exact matches, you need a bulkified before insert, before update Apex trigger to enforce this logic mathematically.
Read article
Salesforce Flow & Automation

Salesforce Basics: Salesforce Order of Execution Made Simple

If you’ve spent any time building automation in Salesforce, you’ve probably heard this Golden Rule: "Always update fields on the triggering record using a Before-Save Flow!" It sounds simple enough, but why is this rule so heavily emphasized? What actually happens under the hood when you update a record inside an After-Save Flow instead? Does Salesforce rewind everything back to Step 1? Let’s lift the hood on the Salesforce Order of Execution, break down what happens during a record update in plain English, and look at how to build faster, cleaner automation. The Basics: The Salesforce "Assembly Line" Whenever a user clicks Save on a record, Salesforce doesn't just write the data to a database and call it a day. It passes the record through a strict, multi-step assembly line called the Order of Execution . Here is a simplified look at what happens in order: Loads the record and applies your new field values....
Read article
Salesforce Security & Access

Salesforce Basics: Formulas, Validation Rules & Order of Execution in Salesforce

💬 In plain words: When you press Save, Salesforce runs a fixed assembly line: checks → before automation → save → after automation → related updates → commit. Half of all tricky bugs are just two steps of this line writing the same field. Learn the line once and those bugs become obvious. 📌 Example: A validation rule demands Discount ≤ 20%. A workflow field update later sets Discount = 25% — and saves fine, because workflow runs AFTER validation. Users can't break the rule; your own automation just did. That's why order matters. 🎬 Real-Life Example: The Field That Kept Changing Itself Back A Flow sets Priority__c to High for same-day parcels. Yet saved records keep showing Normal. The admin swears the Flow runs. The developer swears the trigger works. Both are right. The Old/Bad Way: The before-save Flow sets High. Then a before trigger — written two years earlier — recalculates Priority and overwrites it. Nobody ...
Read article
Topic Apex Triggers Best Practices Bulkification Cheatsheet Salesforce Development Trigger Context Variables Trigger Handler Framework
Apex Triggers Best Practices Bulkification Cheatsheet Salesforce Development Trigger Context Variables Trigger Handler Framework

Salesforce Apex Triggers Cheatsheet: Context Variables, Events & Best Practices

In plain words: An Apex Trigger Cheatsheet is a quick-reference guide that summarizes the essential syntax, context variables, and lifecycle events required to automate business logic when records are created, updated, or deleted in Salesforce. It provides clean code snippets and best practices to help developers write fast, bulkified, and scalable backend automation. Apex triggers are powerful server-side blocks of code that execute before or after database events occur on Salesforce objects. Understanding trigger execution events, context variables ( Trigger.new , Trigger.oldMap ), and architectural standards like the "One Trigger Per Object" rule ensures your code runs efficiently and stays well within platform governor limits.
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.
Read article
Topic Salesforce Admin
Salesforce Platform & Administration

Salesforce Order of Execution: The Complete Modern Guide

In plain words: "Order of Execution" means two different things in Salesforce. On the server , it's the fixed checklist Salesforce follows every time a record is saved: when validation, Flows, triggers, and other automation run. In Lightning Web Components (LWC) , it's the timeline of a component's life: when it's created, added to the page, drawn, updated, and removed. Know both, and you'll always know where your logic belongs. Most automation bugs come down to timing. A field gets overwritten by something that ran later. A trigger fires twice. A component tries to use data before it loads, or an HTML element before it exists. This post covers both timelines: first the record save order, then the LWC order.
Read article
Topic Apex
Apex Development

5 Scenario-Based Apex Trigger Interview Questions

In plain words: Apex Triggers are scripts that execute before or after data is inserted, updated, or deleted in Salesforce. During a developer interview, hiring managers won't just ask you to define a trigger—they will give you real-world business scenarios and ask you exactly when and how you would code the solution. In the world of Salesforce development, Apex triggers are the backbone of backend automation. If you are preparing for a Salesforce Developer interview, you must be ready to talk through business requirements and translate them into scalable, bulkified code. Here are five common scenario-based trigger questions and how to answer them like a pro.
Read article
Topic Apex
Apex Development

Apex Trigger Interview Questions: 3 Real-World Scenarios

In plain words: Scenario-based Apex trigger interview questions assess whether a developer understands the Salesforce Order of Execution, how to write bulkified code that never exceeds governor limits, and when to use before vs. after events properly. During technical interviews for Salesforce Developer and Technical Architect roles, interviewers look beyond basic syntax. They present real-world scenarios to evaluate your understanding of bulkification, recursion management, governor limits, and clean trigger architecture. Below are three classic trigger scenarios with production-ready solutions.
Read article