KEMBAR78
Salesforce Trigger Interview Qa | PDF | Data Management | Software Development
0% found this document useful (0 votes)
13 views6 pages

Salesforce Trigger Interview Qa

The document contains a comprehensive list of Salesforce trigger interview questions and answers, covering basic, intermediate, and advanced topics. It explains the purpose of triggers, their types, context variables, bulkification, recursion, and best practices for writing efficient triggers. Additionally, it includes sample trigger code for various scenarios and emphasizes the importance of using handler classes for maintainable code.

Uploaded by

Rakshitha Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views6 pages

Salesforce Trigger Interview Qa

The document contains a comprehensive list of Salesforce trigger interview questions and answers, covering basic, intermediate, and advanced topics. It explains the purpose of triggers, their types, context variables, bulkification, recursion, and best practices for writing efficient triggers. Additionally, it includes sample trigger code for various scenarios and emphasizes the importance of using handler classes for maintainable code.

Uploaded by

Rakshitha Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Salesforce Trigger Interview Questions & Answers

✪ Basic Trigger Questions (For Freshers)

1. What is a trigger in Salesforce? When is it used?

2. A trigger is Apex code that executes before or after specific data manipulation language (DML)
events occur, such as insert, update, delete, or undelete. Triggers are used when standard
workflows, processes, or flows do not meet business logic requirements.

3. What are the types of triggers in Salesforce?

4. There are two types: Before triggers (used to update or validate values before they are saved to
the database) and After triggers (used to access field values that are set by the system and
affect changes in related records).

5. What is the difference between before and after triggers?

6. Before Trigger: Runs before records are saved to the database. Used to update field values
directly.

7. After Trigger: Runs after records are saved. Used to perform operations that require the record ID
or related objects to be committed.

8. Can you write a simple trigger that updates a field on Account when a Contact is inserted?

trigger UpdateAccountOnContactInsert on Contact (after insert) {


Set<Id> accIds = new Set<Id>();
for(Contact c : Trigger.new) {
if(c.AccountId != null) accIds.add(c.AccountId);
}
List<Account> accList = [SELECT Id, Custom_Field__c FROM Account WHERE
Id IN :accIds];
for(Account acc : accList) {
acc.Custom_Field__c = 'Updated';
}
update accList;
}

9. What are trigger context variables? Name a few.

1
10. They are predefined variables used inside triggers like:

◦ Trigger.new , Trigger.old , Trigger.isInsert , Trigger.isBefore ,


Trigger.isAfter , Trigger.isUpdate , Trigger.newMap , Trigger.oldMap ,
etc.

11. What is Trigger.isInsert and how is it different from Trigger.isBefore ?

12. Trigger.isInsert checks if the operation is INSERT. Trigger.isBefore checks if the


trigger runs before records are saved. You can have combinations like before insert ,
after update , etc.

13. Can you bulkify this simple trigger? Explain how bulkification helps.

14. Bulkification is done by using collections (Sets, Maps) and minimizing DML/SOQL inside loops.
This improves performance and avoids governor limits. See Q4 code as an example.

15. What is recursion in triggers? How do you avoid it?

16. Recursion is when a trigger causes itself to run repeatedly, leading to errors. To avoid it, use
static variables in handler classes to check if the logic has already run.

17. What is the trigger order of execution?

18. The order is:

1. Before triggers
2. Validation rules
3. Save to DB (but not commit)
4. After triggers
5. Assignment rules, auto-response, workflow rules
6. Processes, flows
7. Escalation rules
8. Commit to DB
9. Post-commit logic (email, async jobs)

19. What happens if there's a validation rule and a trigger on the same object?

20. The before trigger runs first. If it makes changes, those are checked against validation
rules. If validation fails, the record is not saved.

✪ Intermediate Questions

1. How do you handle DML inside loops in triggers? Why is it bad?

2. Move DML outside loops by first collecting records in a list. Doing DML inside loops causes
hitting governor limits (like 150 DML per transaction).

3. How do you restrict a trigger from running twice during a transaction?

2
4. Use a static Boolean variable in a helper class to track if the logic has already executed.

5. What are trigger.new and trigger.old? What’s the difference between the two?

6. Trigger.new holds the new version of records (available in insert/update). Trigger.old


holds old values (available in update/delete).

7. Why are after triggers used for update/delete operations?

8. Because in after triggers, records are committed and system-generated fields (like IDs) are
available. Needed for related record operations.

9. What is the difference between trigger.new and trigger.newMap ?

10. trigger.new is a list of sObjects. trigger.newMap is a map of Id to sObject. Use newMap


when you need direct access to records by ID.

✪ Multi-Object Scenario-Based Questions

1. Update Account's custom field "Number_of_Contacts__c" when Contact is inserted or


deleted:

trigger UpdateAccountContactCount on Contact (after insert, after


delete) {
Set<Id> accIds = new Set<Id>();
if(Trigger.isInsert) {
for(Contact c : Trigger.new) if(c.AccountId != null)
accIds.add(c.AccountId);
} else if(Trigger.isDelete) {
for(Contact c : Trigger.old) if(c.AccountId != null)
accIds.add(c.AccountId);
}
List<Account> accs = [SELECT Id, Number_of_Contacts__c, (SELECT Id
FROM Contacts) FROM Account WHERE Id IN :accIds];
for(Account acc : accs) {
acc.Number_of_Contacts__c = acc.Contacts.size();
}
update accs;
}

2. Update Account's Status__c field to “Engaged” when Opportunity is created:

trigger OppTrigger on Opportunity (after insert) {


Set<Id> accIds = new Set<Id>();
for(Opportunity opp : Trigger.new) if(opp.AccountId != null)
accIds.add(opp.AccountId);
List<Account> accList = [SELECT Id, Status__c FROM Account WHERE Id

3
IN :accIds];
for(Account acc : accList) acc.Status__c = 'Engaged';
update accList;
}

3. Update Account's Total_Amount__c when Invoice__c is created:

trigger UpdateAccountAmount on Invoice__c (after insert) {


Map<Id, Decimal> accTotals = new Map<Id, Decimal>();
for(Invoice__c inv : Trigger.new) {
if(inv.Account__c != null) {
accTotals.put(inv.Account__c,
accTotals.getOrDefault(inv.Account__c, 0) + inv.Amount__c);
}
}
List<Account> accList = [SELECT Id, Total_Amount__c FROM Account WHERE
Id IN :accTotals.keySet()];
for(Account acc : accList) acc.Total_Amount__c +=
accTotals.get(acc.Id);
update accList;
}

4. Update Last_Task_Completed__c on Contact when Task is marked completed:

trigger UpdateContactTask on Task (after update) {


Set<Id> contactIds = new Set<Id>();
for(Task t : Trigger.new) {
if(t.Status == 'Completed' && t.WhoId != null &&
String.valueOf(t.WhoId).startsWith('003')) {
contactIds.add((Id)t.WhoId);
}
}
List<Contact> contacts = [SELECT Id FROM Contact WHERE Id
IN :contactIds];
for(Contact c : contacts) {
c.Last_Task_Completed__c = System.today();
}
update contacts;
}

5. Update Has_Closed_Case__c to true on Account if related Case is closed:

trigger CaseTrigger on Case (after update) {


Set<Id> accIds = new Set<Id>();
for(Case c : Trigger.new) {
if(c.Status == 'Closed' && c.AccountId != null)

4
accIds.add(c.AccountId);
}
List<Account> accs = [SELECT Id, Has_Closed_Case__c FROM Account WHERE
Id IN :accIds];
for(Account acc : accs) acc.Has_Closed_Case__c = true;
update accs;
}

✪ Advanced & Best Practices

1. What are trigger frameworks?

2. Frameworks organize trigger logic using handler classes. Helps reuse, test, and maintain code.
Examples: One Trigger Per Object, Trigger Handler Pattern, fflib.

3. What is a helper class in triggers?

4. A helper class contains logic moved out of triggers for reusability and separation of concerns.
Trigger only calls methods from the helper.

5. Best way to handle exceptions in triggers?

6. Use try-catch blocks in helper classes, add debug logs, avoid swallowing exceptions unless
necessary, and notify admins via email or custom logging.

7. If a trigger needs to call multiple future methods, what should you do?

8. Limit future calls to one per transaction where possible. Consider batch apex or queueable
classes instead, which are more flexible and avoid limits.

9. How do you write a trigger that works for both single-record and bulk DML?

10. Always use collections (Sets, Maps, Lists), never rely on a single record (like Trigger.new[0] ).
Avoid SOQL and DML in loops.

11. Can triggers be disabled temporarily?

12. Yes, using Custom Settings/Metadata or a static Boolean flag in handler class (not ideal for long-
term). Can also deploy inactive code to sandbox.

13. Why not write business logic directly inside triggers?

14. Violates clean code principles. Makes unit testing harder. Harder to debug or reuse logic. Use
handler classes instead.

15. How to enforce field-level security in triggers?

5
16. Triggers run in system context, so FLS isn’t enforced. Use
Schema.sObjectType.Contact.fields.Email.isUpdateable() to check manually.

17. Explain a time when you had to refactor a trigger into a handler class.

18. When multiple triggers were being written on a single object, I created a TriggerHandler class,
moved logic into reusable methods, used context variables, and ensured logic was modular and
unit-testable.

✪ Bonus Mock Challenge: Order__c trigger to update Product__c and Account

trigger OrderTrigger on Order__c (after insert) {


Set<Id> prodIds = new Set<Id>();
Set<Id> accIds = new Set<Id>();

for(Order__c ord : Trigger.new) {


if(ord.Product__c != null) prodIds.add(ord.Product__c);
if(ord.Account__c != null && ord.Amount__c > 10000)
accIds.add(ord.Account__c);
}

List<Product__c> prods = [SELECT Id, Last_Ordered_Date__c FROM Product__c


WHERE Id IN :prodIds];
for(Product__c p : prods) p.Last_Ordered_Date__c = System.today();

List<Account> accs = [SELECT Id, VIP__c FROM Account WHERE Id IN :accIds];


for(Account a : accs) a.VIP__c = true;

update prods;
update accs;
}

Let me know if you want these answers formatted into flashcards, quiz format, or need help with
writing test classes!

You might also like