Apex triggers are a powerful feature in Salesforce that allow you to execute custom logic before or after records are inserted, updated, or deleted in the database. They are associated with SObjects, which can be either standard objects such as Account, Contact, Opportunity, etc. or custom objects created to meet specific business requirements.
Triggers are used to perform actions or enforce business rules whenever these events occur. They provide a way to customize and extend the functionality of Salesforce.
The execution context refers to the environment in which an Apex trigger is executed. It includes various aspects such as the order of execution, the record or records being processed, and the values of the fields being modified.
Triggers in Salesforce offer several key features and capabilities:
•\ Custom Logic: Triggers allow developers to write custom logic in Apex code to perform specific actions or enforce business rules when records are created, updated, or deleted.
•\ Data Validation: Triggers can be used to validate data before it is saved to the database. This ensures that the data meets specific criteria or follows certain business rules.
•\ Record Enrichment: Triggers can enrich records by automatically populating fields or performing calculations based on other field values or external data sources.
•\ Related Record Updates: Triggers can update related records when a specific event occurs on a record. For example, when a new Opportunity is created, a trigger can automatically create related Task records for follow-up activities.
•\ Workflow Automation: Triggers can automate complex workflows by performing multiple actions based on specific conditions or events.
•\ Integration with External Systems: Triggers can integrate with external systems or APIs to perform actions or retrieve data from external sources.
•\ Debugging and Logging: Triggers can be debugged and logged to track their execution and troubleshoot any issues.
There are two types of triggers in Salesforce:
\ 1.\ Before Triggers: Before triggers are used to update or validate record values before they are saved to the database. They allow developers to modify data before it is committed to the database. Before triggers are commonly used for data validation and enrichment purposes.
\ 2.\ After Triggers: After triggers are used to access field values that are set by the system after the record is saved to the database. They allow developers to perform actions based on the updated values of the record. After triggers are commonly used for tasks that require access to the updated record data, such as sending notifications or updating related records.
A trigger is defined using the trigger keyword, followed by the trigger name and the object on which the trigger operates. You specify the trigger event like before insert and the trigger code block as in the following example.
Let us create a new trigger on the contact object. Go to Setup ➤ Object Manager tab
➤ Contact ➤ Triggers, and click the “New” button.
trigger ContactTrigger on Contact (before insert) { for (Contact con : Trigger.new) {
// Set standard field values for the newly inserted Contact records
con.firstname= ‘Max’;
con.LastName = ‘Blank’;
}
}