Top Apex Trigger Interview Questions & Answers (2025) — Real Scenarios & Code

A concise guide to the most frequently asked Apex Trigger interview questions in 2025 — real scenarios, code examples, and best practices every Salesforce developer should know.

Why Apex Triggers still matter

Apex Triggers are essential for executing custom business logic that point-and-click tools can’t handle. Interviewers now prioritise problem-solving and real scenarios over textbook definitions — so showing practical knowledge (bulkification, recursion control, context variables) matters more than ever.

Core concepts and examples

1. What is an Apex Trigger? When to use it

An Apex Trigger runs before or after DML events (insert, update, delete, undelete). Use triggers for complex validations, cross-object updates, and scenarios where declarative tools (Flows) are insufficient.

2. Trigger types (before vs after)

Before triggers are ideal for modifying values prior to saving. After triggers are used when the record Id or committed DB state is required.

trigger AccountTrigger on Account (before insert, after insert) {
    if(Trigger.isBefore && Trigger.isInsert){
        for(Account acc : Trigger.new){
            acc.Name = acc.Name + ' - Verified';
        }
    }
    if(Trigger.isAfter && Trigger.isInsert){
        System.debug('Account Created with Id: ' + Trigger.new[0].Id);
    }
}

3. Recursive triggers — prevention

Recursive triggers occur when updates within a trigger cause the same trigger to fire again. The standard fix is a static flag in a helper class to ensure logic runs only once per transaction.

public class TriggerHelper {
    public static Boolean isExecuted = false;
}

trigger ContactTrigger on Contact (before update) {
    if(TriggerHelper.isExecuted) return;
    TriggerHelper.isExecuted = true;
    for(Contact con : Trigger.new){
        if(con.LastName != 'Test'){
            con.LastName = 'Test';
        }
    }
}

4. Bulkification

Always write triggers to handle up to 200 records. Avoid SOQL/DML inside loops; use collections and maps.

Set accIds = new Set();
for(Account acc : Trigger.new){
    accIds.add(acc.Id);
}
Map> accToContacts = new Map>();
for(Contact con : [SELECT Id, AccountId FROM Contact WHERE AccountId IN :accIds]){
    if(!accToContacts.containsKey(con.AccountId)){
        accToContacts.put(con.AccountId, new List());
    }
    accToContacts.get(con.AccountId).add(con);
}

5. Async calls from triggers

You can call @future methods from triggers to separate work into a different transaction (useful for avoiding Mixed DML and offloading non-blocking work).

trigger AccountTrigger on Account (after insert) {
    FutureHandler.sendEmail(Trigger.newMap.keySet());
}

public class FutureHandler {
    @future
    public static void sendEmail(Set accountIds){
        // async logic
    }
}

6. Context variables

Know Trigger.isInsert, isUpdate, isBefore, isAfter, and the Trigger collections (Trigger.new, Trigger.old, Trigger.newMap, Trigger.oldMap) — they are fundamental in conditional logic inside handlers.

7. Order of execution (high level)

  • System validation
  • Before triggers
  • Custom validation rules
  • After triggers
  • Assignment rules, auto-response, workflow
  • Process Builder / Flow
  • DML committed
  • Post-commit async activities

8. Mixed DML

Performing DML on setup and non-setup objects in the same transaction causes a Mixed DML Error. Use @future or Queueable to separate transactions.

9. Trigger design best practices

  • One trigger per object
  • Delegate logic to handler classes
  • Use Maps, Sets, and bulk patterns
  • Protect against recursion
  • Write unit tests with 75%+ coverage

10. Example: Prevent deletion of Accounts with Contacts

trigger AccountBeforeDelete on Account (before delete) {
    Set accIds = new Set();
    for(Account acc : Trigger.old){
        accIds.add(acc.Id);
    }
    Map accWithContacts = new Map(
        [SELECT AccountId, COUNT(Id) cnt FROM Contact WHERE AccountId IN :accIds GROUP BY AccountId]
    );
    for(Account acc : Trigger.old){
        if(accWithContacts.containsKey(acc.Id)){
            acc.addError('Cannot delete Account with Contacts!');
        }
    }
}

Final tips

Interviewers expect scenario-based answers: be ready to write code, highlight bulkification and recursion handling, and when to choose Flow over code.

Why this matters for Salesforce teams

Understanding trigger design and patterns reduces production incidents, improves performance, and ensures maintainable code. For admins, developers, and architects, mastering these topics speeds up development and improves system reliability.

Category: Salesforce Tips