Apex Engineering Track

Salesforce ApexTrigger Interview Questions

The definitive guide to production-grade Apex triggers. Master before vs after trigger details, trigger handler framework setups, and trigger bulkification patterns.

Core Concepts

Master the trigger nuances that separate senior architects from junior developers.

Bulkification & Limits

Moving beyond single-record logic to enterprise-scale processing.

Set/Map Collection Patterns
Avoiding SOQL in Loops
Heap Size Management
10,001 DML Limit

Enterprise Patterns

Building maintainable, testable, and scalable trigger architectures.

Trigger Handler Pattern
Recursion Prevention
Bypass Mechanisms
One Trigger Per Object

Transaction Control

Understanding the Save Order and how triggers interact with the database.

Before vs After Context
Database.insert Partial Success
Async Apex (Queueable/Future)
Transaction Finalizers

Beginner Trigger Questions

Explain before vs after triggers in Salesforce Apex, and when to use each.

Weak Answer

"Before triggers run before the record is saved, and after triggers run after it has been saved to the database."

Strong Answer
Use "Before" triggers to validate input values or modify fields on the triggering record itself; this is highly efficient because changes are applied in memory before writes, avoiding extra DML statements. Use "After" triggers when you need to access system-generated values (like record Id or CreatedDate) or update related records, which does require a DML statement.

What are trigger context variables, and how do you use them in Apex triggers?

Weak Answer

"Context variables are lists of records that tell the trigger what event is currently running."

Strong Answer
Trigger context variables are system-defined variables exposing runtime metadata. Key variables include booleans like Trigger.isBefore and Trigger.isInsert, and collections like Trigger.new (list of new records) and Trigger.oldMap (map of old IDs to records, useful for detecting field updates).

What is trigger bulkification, and how do you write a bulk-safe trigger?

Weak Answer

"Bulkification is writing your code so it doesn't crash when you load a batch of records."

Strong Answer
Trigger bulkification means designing your Apex logic to process batches of records efficiently (up to 200 records per execution block). Writing bulk-safe code requires avoiding SOQL queries and DML updates inside loop structures. Instead, gather query criteria in a Set, perform a single SOQL query outside loops, use a Map to correlate data, and save database commits via collections. Review transaction boundaries on our Governor Limits Explained Guide.

Intermediate Trigger Questions

Why is it a best practice to adhere to the 'One Trigger Per Object' pattern?

Weak Answer

"It keeps your trigger folder clean and makes the files easier to search."

Strong Answer
Salesforce does not guarantee the order of execution when multiple triggers exist on the same object. Having a single trigger per object enforces a predictable execution sequence, simplifies debugging, facilitates trigger bypass configurations, and reduces redundant database queries. Learn trigger designs in our Apex Interview Questions Guide.

What is a Trigger Handler Framework, and how does it separate concerns in Apex?

Weak Answer

"It's a helper class where you write the actual code instead of putting it in the trigger body."

Strong Answer
A Trigger Handler framework separates trigger event routing from business logic. The trigger file itself only contains a single line that calls the framework orchestrator. The orchestrator delegates events (like beforeInsert() or afterUpdate()) to dedicated handler classes, improving code maintainability, promoting reuse, and enabling unit testing without committing dummy test records.

How do you implement trigger recursion prevention in Apex triggers?

Weak Answer

"Use a static boolean variable set to true after the first execution to block other runs."

Strong Answer
While a simple static boolean (e.g., runOnce) works for single records, it fails during bulk DML updates because Salesforce batches records in chunks of 200, which resets the transaction context but retains static variables. A bulk-safe design uses a helper class with a static Set<Id> to store processed record IDs. The handler compares incoming records against the set, filters out already processed records, executes logic, and adds the IDs to the set.

Advanced Trigger Questions

Explain the Salesforce Save Order of Execution and where Apex triggers fit in this flow.

Weak Answer

"Triggers run first, then validations run, then the database saves the records."

Strong Answer
The Save Order of Execution is: 1) System validation and Before-Save Flows run. 2) Before Apex Triggers execute. 3) Custom validation rules execute. 4) Record is written to the database (not committed). 5) After Apex Triggers execute. 6) After-Save Flows execute. Knowing this, updates that occur after validation rules must happen in After-Save paths, whereas initial field sanitization belongs in Before-Save flows. For low-code limits, check our Salesforce Flow Guide.

How do you handle Mixed DML Exceptions in Apex Triggers when updating setup and non-setup records?

Weak Answer

"You just use try-catch blocks to capture the error and retry the insert."

Strong Answer
A Mixed DML Exception occurs when you update a setup object (e.g., User or Group) and a non-setup object (e.g., Account or Contact) in the same transaction block. To resolve this inside triggers, you must isolate one of the DML actions by running it asynchronously. You can invoke a Queueable class or call a helper method decorated with @future to run the setup DML in a separate transaction thread.

What is asynchronous trigger processing, and how do you design triggers to handle high-volume event publishing?

Weak Answer

"Use future methods inside the trigger to run all calculations in the background."

Strong Answer
Asynchronous trigger processing is achieved via Platform Events and Event-Driven architecture. A trigger can publish a Platform Event, which executes an event-based trigger asynchronously in a separate transaction. This isolates complex calculations, avoids database locks, and provides a fresh set of governor limits. Practice designing high-scale integration architectures on our Salesforce Mock Interview Screen.

Scenario-Based Trigger Questions

Scenario: You have a trigger that updates contact fields when an account is updated. However, the contact update trigger updates account fields, causing a loop. How do you resolve this?

Weak Answer

"Use a simple boolean flag in a helper class to stop the contact trigger from updating the account."

Strong Answer
This is a recursive update loop. To resolve this, implement double-guarded entry criteria. In the Account trigger, only update Contacts if relevant Account fields have changed (comparing Trigger.new to Trigger.oldMap). In the Contact trigger, check that Contact values differ before calling DML. Finally, implement a static bypass configuration class to allow programmatically disabling either trigger during updates. Practice resolving these loops on our Salesforce Mock Interview Screen.

Scenario: A batch data load of 10,000 Opportunity records fails with 'Too many SOQL queries: 101' in a related Trigger. How do you troubleshoot?

Weak Answer

"I would reduce the data load batch size from 200 to 50 in the Data Loader."

Strong Answer
Reducing batch size is a temporary workaround. The query is executing inside a loop. Refactor the Trigger Handler to extract the query out of the loop. Gather parent Account IDs in a Set, execute a single query outside the loop, load results into a Map (Map<Id, Account>), and query values in-memory inside the loop, limiting database hits to one query.

Scenario: How do you bypass all Apex triggers programmatically during a large migration?

Weak Answer

"Deactivate triggers in sandbox and deploy them as inactive files."

Strong Answer
Configure a hierarchical Custom Setting or Custom Metadata Type bypass flag. In the Trigger Handler orchestrator, query this bypass flag. If set to true, return immediately from execution. This enables administrators to bypass triggers dynamically for specific data loads without deploying code.

Trigger Architecture Questions

How do you design a trigger to handle partial success during bulk updates?

Weak Answer

"Use try-catch blocks to catch the exception and print the log."

Strong Answer
For bulk updates, use database methods (like Database.update(records, false)) to allow partial success. Inside the trigger, use the addError() method on specific invalid records. This rolls back changes for those specific records and reports failures, while letting valid records commit.

What is the Unit of Work pattern, and how does it optimize trigger DML performance?

Weak Answer

"It is a class that manages your queries and loops to save memory."

Strong Answer
The Unit of Work pattern registers database operations during trigger execution. Instead of executing multiple DML statements on different objects throughout the handlers, the Unit of Work commits all transactions at the end of the trigger in a single block. This optimizes Save Order, minimizes DML statements, and prevents database locking bottlenecks.

How does the virtual database state ($Record) differ between Flow Triggers and Apex Triggers?

Weak Answer

"Apex uses Trigger.new and Flow uses $Record, but they are exactly the same."

Strong Answer
Apex triggers process batches of records via context lists (Trigger.new/Trigger.old). Flow triggers evaluate records individually using the $Record and $Record__Prior variables. Furthermore, modifications to `$Record` in Before-Save flows are automatically committed, while modifying `Trigger.new` requires manual assignment in before trigger events.

Frequently Asked Questions (FAQ)

What are Salesforce Apex triggers?

Apex triggers are database scripts that run before or after DML transactions (inserts, updates, deletes, merges) execute on Salesforce records. They enable developers to build custom validations, propagate related updates, and interface with third-party software.

Why is putting business logic directly in the trigger body considered a red flag?

Putting logic directly inside the trigger body makes code difficult to maintain, test, and reuse. It prevents developers from implementing bypass structures or controlling recursion. Best practices dictate using a Trigger Handler pattern.

How do you handle trigger exceptions gracefully in bulk operations?

Use the addError() method on specific records inside the trigger to display user-friendly error messages on screen without rolling back the entire DML batch, keeping partial updates intact.

What is the CPU time limit and how does it affect triggers?

Apex triggers share the synchronous transaction CPU time limit (10 seconds). To prevent CPU time outs, avoid nested loop structures, use indexing, and offload calculations to Queueable jobs.

How does ForcePilot AI help developers master trigger architecture?

ForcePilot AI assesses your trigger code structure, recursion handling, and bulkification patterns. You can practice responding to recruiter questions in real-time on our interactive Salesforce Mock Interview screen.

Think like a
Technical Lead

Interviewer expectations have evolved. They don't just want to see code that works; they want to see code that is defensive. ForcePilot AI evaluates your ability to handle null pointer exceptions, bulk data scenarios, and complex governor limit constraints.

Bulkification First
Clean Architecture
Recruiter Red Flag

"Avoid putting logic directly in the trigger body. Always use a Handler class. This shows you understand separation of concerns and testability."

Architectural Maturity

Stop Coding
Start Engineering