Table of Contents
Salesforce Triggers is an essential term that you should focus on in your Salesforce development journey. Triggers are necessary to automate the business processes within the Salesforce platform, and there is a high probability that interviewers might ask you questions related to this topic. If you are planning to make a shining career as a Salesforce developer, getting information on this topic would be beneficial.
We appreciate your zeal to learn new things, so we have prepared the most popular Salesforce Trigger interview questions with answers in this blog. We have covered the basic concepts and advanced scenario-based questions to prepare you for the interview. So, let’s explore these questions and get prepared to pave your way to success.
Salesforce Trigger Interview Questions for Beginners
1. What is a Salesforce trigger?
A Salesforce Trigger is a component of Apex code that runs before or after specified data manipulation language (DML) events, such as before object records are inserted into the database, even after records are deleted, and so on. Triggers are used to execute custom actions before or following changes to Salesforce records.
2. What is a Trigger Code?
A Trigger code in Salesforce is an element of Apex code that runs before or after particular data manipulation language (DML) events. These events include Salesforce records insert, update, deletes, and undelete. The trigger code is used to implement custom logic, such as data validation, automation, or modification, in response to various DML events.
3. What are the types of Salesforce Triggers?
There are two types of triggers in Salesforce:
Before Triggers: These are called before the DML process on the database is completed. They are commonly used to validate or change data before it is saved.
After Triggers: These triggers are executed after the DML operation and data temporarily saved into database. They can be used when accessing system-set field values (such as a recordID or LastModifiedDate field) or modifying other documents based on the initial record’s actions.
4. Can you explain the trigger execution order in Salesforce?
Salesforce executes the following in order:
- Executes before triggers.
- Executes validation rules.
- Executes after triggers.
- Assignment rules, auto-response rules, and workflow rules are processed.
- Escalation rules and entitlement rules are applied.
5. What is the Trigger.new and Trigger.old context variable?
Trigger.new: It holds the list of new records to be inserted or updated.
Trigger.old: It has the list of old records values before they were updated or deleted.

6. Can triggers be bulkified in Salesforce?
Yes, triggers should always be written with bulk processing in mind, meaning they should handle multiple records at once. Using loops or SOQL queries inside loops can cause issues with governor limit, so developers need to optimize triggers for bulk operations.
7. What are recursive triggers, and how can you avoid them?
A recursive trigger shows up when a trigger calls itself, that leads to an infinite loop. You can avoid recursion by using a static boolean variable to track whether the Trigger has already run.
8. What is the use of Trigger.isExecuting?
Trigger.isExecuting is a boolean that returns true if the current context is a trigger, that will eventually help to check whether your code is running in a trigger context.
9. State the difference between Trigger.new and Trigger.newMap.
Trigger.new is a list of records with new values. On the other hand, Trigger.newMap is a map of IDs to records. This is useful when accessing records using their IDs for processing.
10. Can you use DML operations in triggers?
Yes, you can use DML operations in triggers. However, the best practice is to limit the use of DML operations to avoid hitting Salesforce governor limits. Using collections to handle bulk records in DML is recommended.
Salesforce Trigger Interview Questions for Professionals
11. How would you stop a Trigger from executing multiple times?
You can utilize static variables to prevent a trigger from being executed more than once. A static variable serves as a flag. You can set this flag after the Trigger has been executed. You can skip the logic on subsequent recursive calls to the Trigger by checking the flag’s value.
This process ensures that the Trigger is not executed repeatedly within the same transaction, preventing undesirable behavior or failures.
12. Can you explain the concept of a Trigger framework? Why is it used?
Trigger framework is a Salesforce Apex design pattern allowing more modular and organized trigger logic management. It usually entails utilizing a handler class that includes the logic and the Trigger calling the relevant function from the handler class. The primary advantages of adopting a trigger framework are:
- Reduces code duplication.
- Improves code reuse.
- It simplifies the trigger logic by keeping the code clean and maintainable.
13. How do you ensure that your Triggers are bulk-safe?
Avoid performing DML actions (insert, update, and delete) or SOQL searches within loops to ensure your triggers are bulk-safe. This can soon exceed Salesforce governor restrictions, mainly when dealing with many records. Alternatively, you should:
- Collect records or data into collections (like lists or maps).
- Perform DML or query operations on the entire collection outside of the loop.
So, Trigger can handle mass activities efficiently without experiencing performance difficulties.
14. What are context variables in Salesforce Triggers?
Context variables in Salesforce triggers give critical information about the state of the records being processed and the runtime context in which the Trigger is executed. Some standard context variables are:
Trigger. New: Contains the most recent versions of the records inserted or changed.
Trigger. Old: Contains previous versions of the records being updated or destroyed.
Trigger.isInsert, Trigger.isUpdate, and Trigger.Delete: Indicate the type of DML activity that prompted the Trigger to run.
Trigger.isBefore, Trigger.isAfter: Determine whether the Trigger executes before or after the DML action.
These variables enable developers to handle multiple scenarios efficiently within a single Trigger.
15. Can you control multiple Triggers for the same object?
Salesforce platform permits you to have numerous Triggers on the same object. However, the hierarchy in which the various triggers are executed is not guaranteed. This can lead to unpredictability in how the triggers interact.
Salesforce recommends that each item have only one Trigger to avoid potential complications. A Trigger Handler class allows you to regulate the sequence and execution of your logic easily.
Scenario-Based Salesforce Trigger Interview Questions
16. Write a trigger that updates a field in a parent Account record when a Contact record is updated.
In this case, I will use the after-update Trigger on the Contact object to update the parent Account. Also, we can loop through the Trigger.new records, check the changes, and then update the associated Account record.
17. Write a trigger to stop the Account deletion record with “Active” status.
I would use a before-delete trigger and add logic to check if the status is “Active.” If it is, I’ll add an error to Trigger.old, preventing the deletion of those records.
18. Write a trigger that creates a follow-up task automatically when an Opportunity is marked as “Closed Won.”
In this scenario, firstly, I’d use an after-update trigger on Opportunity. I will check if the stage has been changed to “Closed Won,” if so, I will create a new task related to that Opportunity.
19. How would you prevent a trigger from running multiple times on the same record?
I would implement a static boolean variable to ensure the Trigger runs only once during a specific transaction. This helps prevent recursion and infinite loops.
20. Can you write a trigger that checks if a Contact has a valid email format during insert/update?
Use a before insert and before update trigger to check if the email follows a valid format using a regular expression. If not, add an error to the Contact record to prevent saving invalid data.
Scenario: Prevent Duplicate Records
21. You need to prevent users from inserting duplicate Account records based on the Account Name. How would you write a trigger to enforce this rule?
trigger PreventDuplicateAccounts on Account (before insert) {
Set newAccountNames = new Set();
for (Account acc : Trigger.new) {
newAccountNames.add(acc.Name);
}
List accList = [SELECT Name FROM Account WHERE Name IN :newAccountNames];
Set accountNames = new Set();
for (Account acc : accList) {
accountNames.add(acc.Name);
}
for (Account acc : Trigger.new) {
if (accountNames.contains(acc.Name)) {
acc.addError('An Account with this name already exists.');
}
}
}
This trigger runs before insert and checks for existing Accounts with the same name, preventing duplicates.
Scenario: Auto-Populate Contact’s Account Name
22. When a Contact is inserted, you need to automatically set its Account Name field based on the associated Account’s Name. How would you do this?
Here, we don’t need to explicitly write code to set the Account Name field. When creating a Contact and associating it with an Account, we use the AccountId field. Once the Contact record is created, Salesforce automatically displays the Account Name in the relationship, rather than the Account ID.
However, if the requirement is to update the associated Account Name with the Contact Name upon Contact creation, or vice versa i mean when a Contact is created and linked to an Account, and requirement is to set the Contact Name with Account Name, then custom logic would be needed.
Scenario: Restrict Deletion of Key Accounts
23. Prevent users from deleting Accounts with Annual Revenue greater than $1M.
trigger PreventHighRevenueAccountDeletion on Account (before delete) {
for (Account acc : Trigger.old) {
if (acc.AnnualRevenue > 1000000) {
acc.addError('You cannot delete accounts with annual revenue over $1M.');
}
}
}
This trigger runs before delete and blocks deletion of high-revenue accounts.
Scenario: Create an Opportunity When an Account is Created
24. When an Account is inserted, automatically create a related Opportunity with a default Stage and Amount.
trigger CreateOpportunityOnAccountInsert on Account (after insert) {
List oppList = new List();
for (Account acc : Trigger.new) {
Opportunity opp = new Opportunity(
Name = acc.Name + ' - Initial Deal',
AccountId = acc.Id,
StageName = 'Prospecting',
CloseDate = Date.today().addMonths(1),
Amount = 5000
);
oppList.add(opp);
}
if (!oppList.isEmpty()) {
insert oppList;
}
}
This after insert trigger creates an Opportunity with a default StageName, CloseDate, and Amount whenever an Account is added.
Scenario: Update Parent Account’s Industry When a Child Account Changes
25. When a child Account’s Industry changes, update the parent Account’s Industry to match the child’s new Industry.
trigger UpdateParentAccountIndustry on Account (after update) {
Set parentAccountIds = new Set();
for (Account acc : Trigger.new) {
if (acc.ParentId != null) {
parentAccountIds.add(acc.ParentId);
}
}
Map parentAccounts = new Map(
[SELECT Id, Industry FROM Account WHERE Id IN :parentAccountIds]
);
List accountsToUpdate = new List();
for (Account acc : Trigger.new) {
if (acc.ParentId != null && parentAccounts.containsKey(acc.ParentId)) {
Account parent = parentAccounts.get(acc.ParentId);
parent.Industry = acc.Industry;
accountsToUpdate.add(parent);
}
}
if (!accountsToUpdate.isEmpty()) {
update accountsToUpdate;
}
}
This ensures that when a child Account’s Industry field is updated, the parent Account’s Industry also updates accordingly.
Scenario: Prevent Contact Deletion if Related Cases Exist
26. Prevent users from deleting a Contact if it has related Cases.
trigger PreventContactDeletionWithCases on Contact (before delete) {
Set contactIds = new Set();
for (Contact con : Trigger.old) {
contactIds.add(con.Id);
}
List conWithCaseList = [SELECT Id, (SELECT Id FROM Cases) FROM Contact WHERE Id IN :contactIds];
Map mp = new Map();
for (Contact c : conWithCaseList) {
mp.put(c.Id, c.Cases.size());
}
for (Contact con : Trigger.old) {
if (mp.containsKey(con.Id) && mp.get(con.Id) != 0) {
con.addError('Cannot delete Contact with related Cases.');
}
}
}
This ensures that Contacts with existing Cases cannot be deleted.
Scenario: Auto-Update Account Type Based on Number of Opportunities
27. When an Account has more than 5 closed won Opportunities, update its Type to “VIP Customer”.
trigger UpdateAccountTypeOnOpportunities on Opportunity (after insert, after update) {
Set accountIds = new Set();
for (Opportunity opp : Trigger.new) {
if (opp.StageName == 'Closed Won' && opp.AccountId != null) {
accountIds.add(opp.AccountId);
}
}
List arList = [
SELECT AccountId, COUNT(Id) oppCount
FROM Opportunity
WHERE StageName = 'Closed Won' AND AccountId IN :accountIds
GROUP BY AccountId
];
System.debug('List Of AggregateResult: ' + arList);
Map oppCountMap = new Map();
for (AggregateResult ar : arList) {
oppCountMap.put((Id)ar.get('AccountId'), ((Long)ar.get('oppCount')).intValue());
}
List accountsToUpdate = new List();
for (Id accId : oppCountMap.keySet()) {
if (oppCountMap.get(accId) > 5) {
accountsToUpdate.add(new Account(Id = accId, Type = 'VIP Customer'));
}
}
if (!accountsToUpdate.isEmpty()) {
update accountsToUpdate;
}
}
This updates an Account’s Type to “VIP Customer” if it has more than 5 closed deals.
Scenario: Populate a Custom Field with Combined First and Last Name
28. Automatically populate a custom field Full_Name__c on Contact with the combination of FirstName and LastName.
On the Contact object, there is already a standard Name field that displays the full name.
So I don’t think there is a need to explicitly create custom field and write custom logic.
Scenario: Log Every Update to a Custom Log Object
29. Whenever an Opportunity is updated, log the changes in a custom Opportunity_Log__c object.
trigger LogOpportunityUpdates on Opportunity (after update) {
List logs = new List();
for (Opportunity opp : Trigger.new) {
logs.add(new Opportunity_Log__c(
OpportunityId__c = opp.Id,
Changed_Fields__c = 'Opportunity Updated',
Updated_Date__c = System.now()
));
}
if (!logs.isEmpty()) {
insert logs;
}
}
This creates a log entry every time an Opportunity is updated.
Scenario: Prevent Changing Stage Back to “Prospecting”
30. Prevent users from changing an Opportunity’s stage back to “Prospecting” once it has been updated.
trigger RestrictStageReversion on Opportunity (before update) {
for (Opportunity opp : Trigger.new) {
Opportunity oldOpp = Trigger.oldMap.get(opp.Id);
if (oldOpp.StageName != 'Prospecting' && opp.StageName == 'Prospecting') {
opp.addError('Cannot revert to Prospecting stage.');
}
}
}
This prevents users from reverting an Opportunity back to the Prospecting stage.

Winding Up
These interview questions can help you prepare for what to expect in the interview, whether you’re a beginner or an experienced professional looking for best practices.
Also, practising the above-mentioned Salesforce trigger interview questions will help you pass your next Salesforce developer interview and land your dream job. If you want to get more insights about Salesforce development, you can join our Slack community.
Join our newsletter: Get daily update on Salesforce career insights & news!
Join Now!