In this article we will discuss about what are triggers in Salesforce? What are their types? and When to use each type of trigger?
- Introduction –
- Triggers are special bundles of code that fire whenever an appropriate database action occurs.
- We can use triggers to allow us to define complex logical processes that occurs whenever a record is changed against Salesforce database.
- We should look to use trigger-based solution when complex logic is involved that either can not be done in Process Builder / Workflow or require the manipulation of data across multiple disparate objects.
- Unlike declarative tools, triggers can be fired on deletion and undeletion of records.
- Types of Apex Triggers –
- Insert: Runs when creating new record.
- Update: Run when updating existing record.
- Delete: Runs when removing a record.
- Undelete: Runs when reinstating a record from the recycle bin.
- A trigger can run in either before context or after context.
- We can not use before context with undelete.
- When to use each trigger type? –
- Before Insert:
- Before insert triggers are first trigger that can run on any record, before record has been assign an Id.
- We should use before insert trigger to set any values on records we are inserting that are based on complex logic.
- We can run before insert trigger if we want to implement any complex validation.
- After Insert:
- After insert trigger run once the record has been saved to the database and been given an Id.
- We should use after insert triggers when we either require Id, need to query for some other calculated field value, or want to create or update additional records.
- We may require Id when creating a series of default child records.
- Before Update:
- Before update runs before any changes made to existing records are saved.
- In this, we have access to both old & new values on the record, that enable us to compare the values and make or validate changes as apppropriate.
- After Update:
- After update trigger fires once updates have been written to database and should be used for either creating or updating related records, or when a value from the save is required.
- Before Delete:
- Before delete trigger runs before a record is deleted from database and should be used to validate that record should be deleted or not and block delete incase of delete is not needed or possible.
- After Delete:
- Once record deletion has saved to the database but not committed, the after delete trigger runs.
- This trigger-type should be used to clearing related records where cascade delete will not occur.
- After Undelete:
- If deleted record is retrieved from recycle bin after it has been resaved to the database, the after delete trigger will fire.
- This trigger will help us to recreate any relationships or set any value we wish on related records.
- Before Insert:
Thanks for reading this article till the end. In next article we will discuss about Trigger Context Variables and Bulkification of triggers.
Leave a comment