When would you use a before trigger instead of an after trigger?
Testing Whether the answer comes from the Id, or from a memorised table.
Before, when I want to change something on the record being saved — default a field, tidy up a phone number, block the save with an error. I get that for free, because the save has not happened yet. After, when I need the record's Id to do anything else, like creating a child record or a task against it.
The question I actually ask is whether the record has an Id yet. In a
before trigger it does not, so Trigger.new is still writable and I can
set fields with no DML at all. In an after trigger the Id exists, and that
is the only reason to be there — related records, anything keyed on Id.
Trigger.new is read-only in an after trigger, and touching it throws
System.FinalException: Record is read-only. Doing the same field default
after the save instead costs an extra update, which re-enters the
trigger and spends part of the 100-query budget on a second pass I did not
need.
They'll ask next Can you validate in a before trigger?
Yes — addError() blocks the save. Called on the record it shows as a
page-level error at the top; called on the field, con.Phone.addError(),
it lands beside that field instead. Either way it beats saving and then
rolling back.
They'll ask next Does Trigger.old exist in a before insert?
No — Trigger.old and Trigger.oldMap are null on insert. So any code
comparing the old value to the new one has to sit behind
Trigger.isUpdate, or it throws a null pointer the first time somebody
loads a file.
They'll ask next Where do before-save flows fit?
Same slot as a before trigger, and faster for a plain field assignment. I keep Apex for the cases that need a query or a loop.