In the world of Salesforce development, Apex triggers are the backbone of backend automation. If you are preparing for a Salesforce Developer interview, you must be ready to talk through business requirements and translate them into scalable, bulkified code.
Here are five common scenario-based trigger questions and how to answer them like a pro.
1. Scenario: Updating Child Records from a Parent
The Question:
"Whenever the 'Status' field on an Account is changed to 'Inactive', we need to automatically update a custom 'Status' field on all related Contact records to 'Inactive'. How would you build this?"
The Solution:
You must use an After Update trigger. Because you are modifying records other than the ones that initiated the trigger, you cannot use a Before trigger.
- First, loop through
Trigger.newto find Accounts where the Status changed to Inactive (comparing againstTrigger.oldMap). - Store the IDs of those Accounts in a
Set<Id>. - Use a single SOQL query to find all Contacts associated with those Account IDs.
- Loop through the queried Contacts, update their Status, add them to a
List<Contact>, and perform a singleupdateDML operation at the very end to ensure bulkification.
2. Scenario: Preventing Record Deletion
The Question:
"We want to prevent users from deleting an Opportunity if its stage is 'Closed Won'. How would you achieve this?"
The Solution:
This requires a Before Delete trigger. You must validate the data before it is permanently removed from the database.
- Loop through the records in
Trigger.old(sinceTrigger.newis not available in delete contexts). - If the Opportunity Stage equals 'Closed Won', use the
addError()method on that specific record. - Using
addError()will safely block the deletion and present a clear error message to the user on the UI.
3. Scenario: Roll-up Summary on a Lookup Relationship
The Question:
"We have a custom object 'Project' and a child object 'Time Entry' connected via a Lookup relationship. We need to calculate the total hours logged on all Time Entries and display it on the Project. Standard Roll-up Summaries aren't available for Lookups. How do you solve this?"
The Solution:
You need an After Insert, After Update, After Delete, and After Undelete trigger on the child Time Entry object.
SUM(Hours__c)) grouped by the Project ID. Finally, update the Project records with the new calculated totals. Mentioning "Aggregate SOQL" will score you major points!
4. Scenario: Complex Field Validation
The Question:
"A user should not be able to create a Lead if the email address already exists on another Lead in the system. Standard matching rules aren't working for our complex criteria. How would you handle this in Apex?"
The Solution:
This requires a Before Insert and Before Update trigger to validate the data before it hits the database.
- Loop through
Trigger.newand extract all the incoming email addresses into aSet<String>. - Run a SOQL query to find any existing Leads in the database that match those emails, storing the results in a
Map<String, Lead>. - Loop through
Trigger.newa second time. If their email exists in the Map, useaddError()to block the save.
Interviewers love this question because junior developers will often put the SOQL query inside the
for loop to check emails one by one. Always emphasize that you will gather the emails in a Set first, and run exactly one SOQL query outside the loop to avoid hitting the 100 SOQL query governor limit!
5. Scenario: Cross-Object Field Updates
The Question:
"When an Account's 'Billing State' is updated, we need to query a custom 'Tax Rate' object based on that state, and update a 'Tax Rate' field on the Account. How is this done?"
The Solution:
This should be handled in a Before Update trigger. Because you are modifying the same record that initiated the trigger, using "Before" allows you to change the field without requiring a secondary DML update statement.
- Collect the new Billing States into a Set.
- Query the Custom Tax Rate object for those states and map them out.
- Loop through
Trigger.new, find the matching Tax Rate in your map, and simply assign it:acc.Tax_Rate__c = matchedRate.Rate__c;.
- One Trigger Per Object: Use a handler framework to keep logic clean.
- Bulkify Everything: Assume
Trigger.newwill always have 200 records. - No SOQL in Loops: Always gather IDs in a Set and query outside the loop.
- No DML in Loops: Add records to a List and update them all at once at the end.
Conclusion
Mastering scenario-based interview questions requires more than just knowing Apex syntax; it requires a deep understanding of the Salesforce order of execution, governor limits, and bulkification patterns. By understanding why you use a "Before" trigger versus an "After" trigger, and how to safely query related data, you will easily stand out in your next technical interview.