Types of Triggers:
Apex Triggers can be classified into two types:
1. Before Triggers
2.After Triggers.
1. Before Triggers:
Before Triggers are executed before the database operation is performed, such as before a record is inserted, updated, or deleted. The trigger code can access the record before it’s saved to the database, allowing developers to modify the record data before the database operation is performed.
Purpose:
Before Triggers are used to enforce data validation rules, such as ensuring that certain fields are populated before a record is saved, or to automatically populate fields based on other field values.
Access to Records:
Before Triggers have access to both the old and new versions of the record, allowing developers to make changes to the record data before it’s saved to the database.
Validation Rules:
Before Triggers can be used to enforce validation rules, such as ensuring that certain fields are populated before a record is saved, or to automatically populate fields based on other field values.
2. After Triggers:
After Triggers are executed after the database operation is performed, such as after a record is inserted, updated, or deleted. The trigger code can access both the old and new versions of the record, allowing developers to perform additional actions, such as updating other records or sending notifications.
Purpose:
After Triggers are used to perform additional actions after a record is saved, such as updating other records, sending notifications, or updating fields.
Access to Records:
After Triggers have access to both the old and new versions of the record, allowing developers to perform additional actions based on the record data after it’s saved to the database.
Additional Actions:
After Triggers can be used to perform additional actions, such as updating other records, sending notifications, or updating fields.