Salesforce Developer Interview Question

When do you use a before trigger and when do you use an after trigger?

What the interviewer is probing, how to structure your answer, and a spoken example you can adapt.

Quick answer

Use a before trigger to validate or change fields on the records being saved, because you can edit Trigger.new directly with no DML and the change saves with the record. Use an after trigger when you need the record id or system fields, or when you are changing other records, such as creating children or updating a parent. Updating the triggering records themselves in an after trigger needs extra DML and risks recursion.

Why interviewers ask this

This is a fundamentals question with a real performance and correctness angle. The interviewer wants to know whether you understand the save lifecycle: before triggers modify records for free, while after triggers see saved ids and work on related records. Explaining why updating the same records in an after trigger is wasteful, and noting that before save flows suit simple field updates, shows you understand the platform rather than repeating habits.

How to structure your answer

  • Explain what you can and cannot do in each context.
  • Give typical uses for before triggers.
  • Give typical uses for after triggers.
  • Mention recursion, extra DML and before save flows.

Example answer

Spoken example, first person

The difference is where you are in the save. In a before trigger the records have not been written yet, so I can change fields on Trigger.new directly and the values save with the record, with no DML at all. That makes before triggers the place for defaulting values, normalising data, or validation where I add an error to the record. In an after trigger the records have been saved, so they have ids and system fields, but Trigger.new is read only. That makes after triggers the place for anything involving other records: creating child records that need the parent's id, updating a parent, or publishing an event. The mistake I see is field updates on the triggering records in an after trigger, which means querying them again and issuing another update. That uses extra DML and fires the triggers again, so you need recursion control for something a before trigger would have done for free. For simple same record field updates, a before save record triggered flow is also a good, efficient option now.

Walking into this interview soon? GhostPilot listens to your live call, spots the question the moment it is asked, and puts a structured answer on your screen in real time. Try it on your next mock, or grab a $29 Session Pass, no subscription, for the real thing.

See how it works

Follow-up questions to expect

  • Why can you not get the record id in a before insert trigger?
  • How do you add a validation error to a record from a trigger?
  • When would a before save flow be better than a before trigger?

Related salesforce developer questions

Your interviewer will ask their own version of this. Paste your actual job description into the free Question Predictor and get the 20 questions that role is most likely to ask, with what each one is really probing.

Predict my questions

Rehearse the hard questions before they are asked

Practise with a live copilot, then walk in ready. A $29 Session Pass gets you through the interview with no subscription and no lock-in.

Get GhostPilot