Development questions
What is the difference between before triggers and after triggers?
Before triggers run before the record is saved to the database. Use them to validate or modify field values on the records in Trigger.new without an extra DML, since changes are saved automatically. Record Ids are not yet available on insert.
After triggers run after the record is saved (but not committed). Record Ids and system-populated fields (CreatedDate, formula values) are available. Use them to create or update related records. Records in Trigger.new are read-only.
Rule of thumb: before = same-record changes and validation; after = related-record changes and logic needing the Id.
apex-triggertriggerfundamentals
More Development questions
- What is an Apex Trigger in Salesforce and when should you use it over Flows?
- Explain all Trigger context variables (isInsert, isUpdate, isDelete, isUndelete, isBefore, isAfter, isExecuting, size, operationType).
- What is the difference between Trigger.new and Trigger.old?
- In which trigger events are Trigger.new and Trigger.newMap available?
- In which trigger events are Trigger.old and Trigger.oldMap available?
- Why is Trigger.newMap not available in before insert?