What is an Apex trigger?

A trigger is the piece of code that executed before and after a record is Inserted/Updated/Deleted from the force.com database. Apex can be invoked through the use of triggers. A Trigger is a functional action which gets on particular events. Triggers will happen before records entering into the database and while goint out of the database.  Each event is the firing point.

  • Onclick
  • Submit.

A Trigger is Apex code that execute before or after the following types of operations.

  • Insert
  • Update
  • Delete
  • Undelete

We can have a trigger run before an Object’s records are inserted into the database, after records have been deleted, or even after a record is restored from the Recycle BIN.


Trigger <trigger name> on <Object name> (trigger Events) {
    // Implement the Logic here

Triggers can be divided into two types.

Types of Triggers:
– Before Triggers
– After Triggers

Before Trigger: Before triggers are used to perform the logic on the same object and specifically we cannot use the DML operation (Insert, update, delete) on these triggers. These triggers fired before the data saved into the database.

After Trigger: After triggers are used to perform the logic on the related objects and these triggers are used access the fields values that are created by system (Ex: CreatedBy, LasteModifiedBy , Record Id etc..).

Bulk Triggers: By default, every trigger is a bulk trigger which is used to process the multiple records at a time as a batch. For each batch of 200 records.

Trigger Context Variables:

All the trigger context variables prefixed with “Trigger.” (Ex: Trigger.isInsert, etc..)

  • isInsert: Returns true if the trigger fired due to insert operation
  • isUpdate: Returns true if the trigger fired due to the update operation.
  • isDelete: Returns true if the trigger fired due to delete operation.
  • isBefore: Returns true if the trigger fired before the record saved.
  • isAfter: Returns true if the trigger fired after the record saved.
  • New: Returns a list of the new version of sObject records.
  • Old: Returns a list of an old version of sObject records.
  • NewMap: Returns a map of a new version of sObject records. (map stored in the form of map<id,newRecords>)
  • OldMap: Returns a map of an old version of sObject records. (map stored in the form of map<id, oldRecords>)
  • Size: Returns an integer (total number of records invoked due to trigger invocation for the both old and new)
  • isExecuting: Returns true if the current apex code is a trigger.

The below table tells about the events we can use in the new trigger and old trigger

Trigger EventTrigger.NewTrigger.Old
Before InsertYesNo
Before UpdateYesYes
Before DeleteNoYes
After UnDeleteNoYes
After InsertYesNo
After UpdateYesYes
After DeleteNoYes

Trigger Context Variable considerations:
– Trigger.Old is always read-only
– We cannot delete trigger.new
– In before triggers, trigger.new can be used to update the fields on the same object.
– In After trigger, we get a runtime exception when the user tries to modify the fields in the same object.