Triggers in Apex

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:

  1. 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.

  2. 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

  1. before insert

  2. after insert

  3. before update

  4. after update

  5. before delete

  6. after delete

  7. 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.new

  • 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

  1. Insert/update/upsert/Delete Record

  2. Executes all before triggers

  3. Record is saved to Database but not commited yet

  4. Execution of all After Trigger

  5. 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 EventTrigger.newTrigger.old
Before InsertModifiableNot Available
After InsertRead-onlyNot Available
Before UpdateModifiableAvailable (Read-only)
After UpdateRead-onlyAvailable (Read-only)
Before DeleteNot AvailableAvailable (Read-only)
After DeleteNot AvailableAvailable (Read-only)
After UndeleteAvailable (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 in Trigger.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 or Trigger.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.