Now when you insert new Contact records, this trigger ensures that the FirstName is set to “Max” and the LastName is set to “Blank” before the records are actually inserted into the Salesforce database. You can test the Apex trigger in your Salesforce environment by creating the new contact.
Trigger events indicate when the trigger code should execute in relation to the database operation. Common trigger events include (before insert), (before update), (before delete), (after insert), (after update), (after delete), and (after undelete).
When multiple triggers are defined for the same object and event, Salesforce determines the order in which they are executed. The order of execution primary include but not limited to:
\ 1.\ System Validation Rules
•\ Salesforce performs system validation, such as verifying that all required fields have a non-null value and that unique fields have unique values.
\ 2.\ Before Triggers
•\ All before triggers are executed.
•\ Custom validation rules are executed.
\ 3.\ After Triggers
•\ All after triggers are executed.
\ 4.\ Assignment Rules
•\ If the record was updated in the transaction, assignment rules are executed.
\ 5.\ Auto-Response Rules
•\ If the record was created and meets the criteria for auto-response rules, these rules are executed.
\ 6.\ Workflow Rules
•\ Workflow rules are executed.
\ 7.\ Processes and Flows
•\ Processes and Flows are executed.
\ 8.\ Escalation Rules
•\ If the record is associated with an entitlement and meets the criteria for escalation rules, these rules are executed.
\ 9.\ DML Operations to Database
•\ Commit DML operations to Database.
\ 10.\ Post-Commit Logic
•\ Any post-commit logic, such as sending emails or updating external systems, is executed.
It’s important to note that the execution order can be influenced by factors such as the type of operation being performed and whether the trigger is defined at the object or field level.
Context variables in Apex are special variables that provide information about the current execution context of your code. They contain data related to the environment in which your code is running, such as trigger events, records being processed, and user information. These context variables help you understand and respond to different situations during code execution.
In the context of Apex triggers, there are several context variables that are commonly used to access information about the trigger’s execution. Here are the main context variables and their purposes:
Trigger.new and Trigger.old
These context variables are used to provide access to the new and old versions of records in a trigger:
•\ Trigger.new: Returns a list of the new versions of the sObject records.
This list is available in insert, update, and undelete triggers.
•\ Trigger.old: Returns a list of the old versions of the sObject records.
This list is available in update and delete triggers.
These context variables allow developers to compare the old and new values of records and perform actions based on the changes. For example, in an update trigger, you can use Trigger.new and Trigger.old to identify which fields have changed and take appropriate actions.