11 - Trigger : Part 3

 When to use After and Before How to decide :


Trigger TypeUse When You Need To...Typical Scenarios
Before
- Modify the same record's fields before saving without a DML statement- Auto-populate or set default values
- Validate data before it’s committed to the database- Enforce validation rules
- Prevent DML with errors (addError() method)- Prevent record saving based on conditions
After
- Work with related records or perform actions needing record existence in the database- Update related records (like updating Account after Contact save)
- Access fields only available after saving (like Id or RecordTypeId)- Perform complex logic needing committed data
- Execute additional operations requiring DML (e.g., creating new records based on the saved record’s details)- Send notifications, make callouts

This table can be a quick reference guide for choosing between before and after triggers based on your needs.


Hint : Triggers are by default Bulkified . so if I have to execute 5000 record insert it gets divided into batch of 200 and 25 batches will run .


Hint : All triggers are bulkified by default and upto 200 records can enter the trigger at once'  (so this mean if 1000 record ´s of Account is updated at once then it assumes 200 one time and pass it to trigger for execution in one go )

Comments

Popular posts from this blog

API Names in Salesforce

18 - LWC - BEST PRACTICES - For accessing HTML elements and their values in JavaScript

6 - Object Relationships (Lookup and Master-Detail and Junction Objects)