Salesforce Apex Trigger Interview Questions and Answers

Salesforce Apex Trigger Interview Questions and Answers

Salesforce Apex Triggers are one of the most important topics in Salesforce interviews. Whether you are preparing for an Admin-to-Developer transition or an experienced developer interview, trigger-related questions are asked frequently.

In this article, we will cover the most commonly asked Apex Trigger interview questions with simple explanations.


1. What is an Apex Trigger?

An Apex Trigger is Apex code that executes automatically before or after Salesforce records are inserted, updated, deleted, or undeleted.

Example

trigger AccountTrigger on Account (before insert) {
    for(Account acc : Trigger.new){
        acc.Description = 'Created Automatically';
    }
}

2. What are the Trigger Events?

Salesforce supports the following trigger events.

  • before insert
  • before update
  • before delete
  • after insert
  • after update
  • after delete
  • after undelete

3. Difference Between Before Trigger and After Trigger

Before Trigger

Used when you want to modify the record before it is saved.

Example:

  • Update Name
  • Update Description
  • Set Default Values

After Trigger

Used when the record ID is required or when working with related records.

Example:

  • Create Child Records
  • Send Email
  • Update Related Objects

4. What is Trigger.new?

Trigger.new contains the new version of records.

Example

for(Account acc : Trigger.new){
    System.debug(acc.Name);
}

5. What is Trigger.old?

Trigger.old contains the old version of records before they were updated or deleted.

Example

for(Account acc : Trigger.old){
    System.debug(acc.Name);
}

6. Difference Between Trigger.new and Trigger.old

Trigger.newTrigger.old
New ValuesOld Values
Insert & UpdateUpdate & Delete
Editable in Before TriggerRead Only

7. What is Trigger.newMap?

Trigger.newMap is a Map<Id, SObject> that stores new records using the record Id as the key.

Example

Account acc = Trigger.newMap.get(recordId);

8. What is Trigger.oldMap?

Trigger.oldMap stores old records using the Id as the key.

Example

Account oldAcc = Trigger.oldMap.get(recordId);

9. Why Should We Avoid SOQL Inside a Loop?

SOQL inside a loop can exceed Salesforce Governor Limits.

❌ Bad Example

for(Account acc : Trigger.new){
    Opportunity opp = [SELECT Id FROM Opportunity WHERE AccountId=:acc.Id];
}

✅ Good Example

Set<Id> accountIds = new Set<Id>();

for(Account acc : Trigger.new){
    accountIds.add(acc.Id);
}

List<Opportunity> opps = [
SELECT Id, AccountId
FROM Opportunity
WHERE AccountId IN :accountIds
];

10. What is Bulkification?

Bulkification means writing Apex code that works efficiently for one record as well as hundreds of records without hitting governor limits.


Frequently Asked Questions

Can we call a Trigger manually?

No. Triggers execute automatically when DML operations occur.

Can we have multiple triggers on one object?

Yes. However, Salesforce does not guarantee the execution order of multiple triggers on the same object.

Can we write SOQL inside a Trigger?

Yes, but avoid writing SOQL inside loops.


Conclusion

Apex Triggers are one of the most frequently asked Salesforce interview topics. Understanding Trigger Events, Trigger.new, Trigger.old, Trigger Maps, Bulkification, and Governor Limits will help you clear Salesforce Developer interviews with confidence.


Tags: Salesforce Apex Trigger, Apex Trigger Interview Questions, Salesforce Interview Questions, Apex Tutorial, Trigger.new, Trigger.old, Bulkification, Salesforce Developer Interview.

Post a Comment

0 Comments