The title pretty much says it all.
Depends on your usage. From my experience here's the break down:
- I'm updating the record that's being updated/inserted - or doing something based on the record being modified
- Examples: Set value of a pick list based on criteria. Send apex e-mail based on the record updated/inserted
- I'm updating or creating records that are NOT being updated/inserted
- Examples: Create a task of an Opportunity that's been edited, Change a look up value on a related record from the Opportunity being edited
The main thing to consider is that the Before happens before the data has been written to the server. This means you can modify the records in "Trigger.new" without having to call a separate "Update." This is ideal if you want to modify data in the records within Trigger.new
After happens after the data has been written to the server. This is important when wanting to create additional related records (Can't create a related record until AFTER the parent has been inserted).
I find about 95% of all the triggers I have ever written are Before Update/Insert. It's very rare that I need a trigger that runs AFTER.
Updating the records being handled by the trigger must be done in the before trigger.
Validating the records being handled by the trigger should ideally be done in the after trigger. At this point no other data changes on the trigger records are admitted (for example by other triggers) - this is the safest point at which to validate the data.
Initiating updates on related records (including chatter posts), sending emails etc., can be done either in before or after triggers. For example:
- if you need a record id (on an insert) to use in a lookup field in a related object, then this needs to be done in the after trigger
- if you want to insert a related record and use its id in a lookup field on the trigger record, then this has to be done in the before trigger
- if you need to use the record autonumber/name (e.g. in an email) then this must be done in the after trigger