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

Published On: August 29, 2025

If you’re preparing for a Salesforce Developer Interview, chances are you’ll face Apex Trigger questions.
Interviewers don’t ask definitions anymore – they want real-time problem-solving, best practices, and coding scenarios.

I’ve compiled the most commonly asked Apex Trigger Interview Questions along with detailed answers & examples 👇

1️⃣ What is an Apex Trigger? When do you use it?

What they ask:
👉 Define a trigger and explain real-time use cases.

Answer:
An Apex Trigger is a piece of Apex code that executes before or after DML events (insert, update, delete, undelete).
We use triggers when:

  • We need to enforce complex business logic.
  • We need to maintain data integrity across objects.
  • Point-and-click automation (Flow, Process Builder) cannot handle the scenario.

Example use case:
Update Contact Status to “Active” when an Account is marked as Active.


2️⃣ What are the types of Triggers in Salesforce?

  • Before Triggers → Used to update/validate values before saving to DB.
  • After Triggers → Used to access record values from DB (Id available).

Example:

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️⃣ What is a Recursive Trigger? How do you prevent it?

What they ask:
👉 They’ll test if you know how to handle infinite loops.

Answer:
A recursive trigger happens when the trigger updates a record, causing itself to fire repeatedly.

Solution: Use a static variable in a helper class.

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️⃣ How do you handle Bulkification in Triggers?

What they ask:
👉 Show that you can handle 200 records at once.

Answer:
Always write triggers in a bulk-safe way:

  • Avoid SOQL/DML inside loops.
  • Use collections (Set, Map, List).

Bad Code ❌:

for(Account acc : Trigger.new){
   Contact con = [SELECT Id FROM Contact WHERE AccountId = :acc.Id LIMIT 1];
}

Good Code ✅:

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

5️⃣ Can you call a Future Method from a Trigger?

Answer:
✅ Yes, but only from an @future(callout=true/false) method.
⚠️ You cannot call another future method or batch from a future.

Example:

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

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

6️⃣ What is a Context Variable in Triggers?

Common Context Variables:

  • Trigger.isInsert, Trigger.isUpdate, Trigger.isDelete
  • Trigger.isBefore, Trigger.isAfter
  • Trigger.new, Trigger.old, Trigger.newMap, Trigger.oldMap

Example:

if(Trigger.isBefore && Trigger.isUpdate){
    for(Account acc : Trigger.new){
        Account oldAcc = Trigger.oldMap.get(acc.Id);
        if(acc.Name != oldAcc.Name){
            System.debug('Account name changed!');
        }
    }
}

7️⃣ What is the Order of Execution in Salesforce?

Answer:
Interviewers LOVE this. Be clear.

  1. System validation rules.
  2. Before triggers.
  3. Custom validation rules.
  4. After triggers.
  5. Assignment rules, Auto-response, Workflow rules.
  6. Process Builder / Flow.
  7. DML committed to DB.
  8. Post-commit logic (Email, Async Apex).

8️⃣ Can you perform DML on Setup & Non-Setup objects in the same transaction?

Answer:
❌ No → This causes MIXED DML ERROR.
✔ Fix → Use @future or Queueable to separate transactions.


9️⃣ What is the Best Practice for Trigger Design?

  • One Trigger per object.
  • Logic should go into a Handler Class (not inside trigger).
  • Use Maps/Sets for bulk operations.
  • Always handle recursion.
  • Write Test Classes (75%+ coverage).

🔟 Write a Trigger to prevent deletion of Accounts with Contacts.

trigger AccountBeforeDelete on Account (before delete) {
    Set<Id> accIds = new Set<Id>();
    for(Account acc : Trigger.old){
        accIds.add(acc.Id);
    }
    
    Map<Id, AggregateResult> accWithContacts = new Map<Id, AggregateResult>(
        [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 focus on scenarios, not definitions.
  • Be ready to write trigger code live.
  • Always mention bulkification + recursion handling.
  • Talk about using Flow instead of Trigger where possible (best practice).

👉 This blog post gives you a full coverage of Apex Trigger Interview Questions that recruiters and interviewers actually ask in 2025.

📚 More Resources for You

Here are some resources to go deeper:

🔗 100 Scenarios (1–4 YOE)
🔗 100 Scenarios (4–8 YOE)
🔗 LWC Q&A (Real Answers Explained)
🔗 600+ Qs from Recruiter Calls
🔗 TCS, Infosys, EY Interview Qs
🔗 Salesforce Project – Hindi + English

TrailheadTitans

At TrailheadTitans.com, we are dedicated to paving the way for both freshers and experienced professionals in the dynamic world of Salesforce. Founded by Abhishek Kumar Singh, a seasoned professional with a rich background in various IT companies, our platform aims to be the go-to destination for job seekers seeking the latest opportunities and valuable resources.

Related Post

Interview Q & A

🚀 Top 100 Scenario-Based Salesforce Developer Interview Questions (2025)

By TrailheadTitans
|
August 27, 2025
Salesforce Jobs, Interview Q & A

🚀 Top 50 Salesforce Developer Interview Questions (2025 Updated)

By TrailheadTitans
|
August 26, 2025
Blog, Interview Q & A

🚀 15 Real Salesforce Developer Interview Scenarios with Answers & Code (2025)

By TrailheadTitans
|
August 25, 2025

Leave a Comment