Skip to main content

5 Common Scenario-Based Apex Trigger Interview Questions

In plain words: Apex Triggers are scripts that execute before or after data is inserted, updated, or deleted in Salesforce. During a developer interview, hiring managers won't just ask you to define a trigger—they will give you real-world business scenarios and ask you exactly when and how you would code the solution.

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.new to find Accounts where the Status changed to Inactive (comparing against Trigger.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 single update DML 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 (since Trigger.new is 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.

Interview Tip: Explain that you will collect the Parent IDs from the child records being modified. Then, run an Aggregate SOQL query (using 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.new and extract all the incoming email addresses into a Set<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.new a second time. If their email exists in the Map, use addError() to block the save.
Developer Trap: SOQL inside a Loop
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;.
360 Card: The Golden Rules of Apex Triggers
  • One Trigger Per Object: Use a handler framework to keep logic clean.
  • Bulkify Everything: Assume Trigger.new will 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.