Triggers in Apex
Table of contents
- Syntax
- Types of events in Trigger
- Trigger context variable
- Trigger Order of Execution
- Important Points to know about Trigger
- Behavior of Trigger.new and Trigger.old in different trigger
In Salesforce, a trigger is a piece of Apex code that executes automatically before or after specific data manipulation language (DML) operations, such as inserting, updating, or deleting records. Triggers allow you to perform custom actions when certain events occur on Salesforce records.
Triggers are primarily used for:
Before Triggers: These are executed before the data is saved to the database. They are useful for validating or modifying record values before they are committed to the database. For example, a before trigger might be used to automatically set a value for a field before saving the record.
After Triggers: These are executed after the data is saved to the database. They are typically used for operations that need to happen after the record is committed to the database, such as updating related records or sending notifications.
Syntax
A trigger is defined on an object and associated with one or more DML events. Here’s the general syntax for an Apex trigger:
trigger TriggerName on ObjectName (trigger_events) {
// Trigger logic goes here
}
Types of events in Trigger
before insert
after insert
before update
after update
before delete
after delete
after undelete
Trigger context variable
Context variables provide information about the trigger context, such as whether the trigger is executing before or after the record is saved, whether it is handling an insert, update, delete, or undelete operation, and more.
Trigger.old
Trigger.newMap
Trigger.oldMap
Trigger.isInsert
Trigger.isUpdate
Trigger.isDelete
Trigger.isBefore
Trigger.isAfter
Trigger.isUndelete
Trigger.isExecuting
Trigger.size
Trigger Order of Execution
Insert/update/upsert/Delete Record
Executes all before triggers
Record is saved to Database but not commited yet
Execution of all After Trigger
Commits all the DML operations to the database
Important Points to know about Trigger
Before Trigger
You cannot perform DML operations (like insert, update, delete, or upsert) on the same records within a before trigger in Salesforce.
Why is this? The before trigger is designed to run before a record is saved to the database, and its primary purpose is to allow you to modify the record's field values before it is committed to the database.
However, attempting to perform a DML operation on the same record inside the before trigger would result in an error because the record is still in the process of being saved, and DML operations on the same record within the same transaction would lead to an infinite loop or conflict.
After Trigger
- The records that fire the after trigger are read-only.
Behavior of Trigger.new
and Trigger.old
in different trigger
Trigger Event | Trigger.new | Trigger.old |
Before Insert | Modifiable | Not Available |
After Insert | Read-only | Not Available |
Before Update | Modifiable | Available (Read-only) |
After Update | Read-only | Available (Read-only) |
Before Delete | Not Available | Available (Read-only) |
After Delete | Not Available | Available (Read-only) |
After Undelete | Available (Read-only) | Not Available |
Read only means we cannot perform DML as well as we cannot update their values
You cannot modify the values in that collection. For example, in an after update trigger, if
Trigger.new
is read-only, you cannot change the values of the records inTrigger.new
.You cannot perform DML operations directly on those records. For instance, you cannot call
update
Trigger.new
in an after update trigger because the records are already committed to the database. DML operations can only be done on new records or those that you explicitly retrieve and modify in code.
Can we use DML statement on before trigger for different object
Yes, you can use DML statements in a before trigger for a different object, as long as you're modifying records that are not part of the trigger's
Trigger.new
orTrigger.old
collections.
Can we use DML statement for same object in before trigger
- In a before trigger, you cannot perform DML operations on the same object (i.e., the object on which the trigger is fired). This is because, in a before trigger, the records in
Trigger.new
are not yet committed to the database.