SalesforceBlue

Feel the rhythm of Salesforce

Apex

Trigger Context Variables Simplified

As the name said Trigger context variables, tell you the current execution context whether the code is executing in insert operation context, update operation context or delete operation context.

If you wanted to relate context then consider it as a street. Now answer in which street this code is running. Whether it’s running in Insert street, Update street, or Delete street.

Now to identify this street or Context technically, there are certain variables that define the current executing context.

Below table list out the different context variable and their significance.

Trigger ContextsDescription
Trigger.isBefore Return true if code is running for before Trigger context i.e. Before any records are Saved. Example – Before Insert
Trigger.isAfterReturn true if code is running for after Trigger context I.e After any records are Saved. Example – After Insert
Trigger.isInsertReturn true if code is running for the insert operation.
Trigger.isUpdateReturn true if code is running for the update operation.
Trigger.isDeleteReturn true if code is running for the delete operation.
Trigger.isUndeleteReturn true if code is running for undelete operation. (Undelete refer to restoring deleted records)
Trigger.newReturn a list of a new version of sObject records. This list contains the new values of the fields changed during an operation.

Trigger.new is available for Before insert, after insert, before update, after update, undelete.

Trigger.new is null in Delete trigger because in delete operation there will not be a new version of any record.
Trigger.oldReturn a list of an old version of sObject records. This list contains the old value of the fields changed during an operation.

Trigger.old is available for before update, after update, before delete, after undelete.

Trigger.old is null in before insert and after insert because there is no version of old or updated records.
Trigger.newMapReturn a map of Ids to a new version of sObject records. This map contains the new value of the fields changed during an operation.

Trigger.newMap is available for after insert, before update, after update, after undelete triggers.

Trigger.newMap is null for before insert because records are not saved in before insert calls so record id is not generated yet.
Trigger.oldMapReturn a map of Ids to an old version of sObject records. This map contains the old value of the fields changed during an operation.

Trigger.oldMap is available for before update, after update, before delete and after delete.

Trigger.oldMap is null in the Insert context because records are inserted for the first time in the database without any updates.

Lets’s see the context variables in action.

trigger ContactTrigger on Contact (before insert, before update) {

    if(Trigger.isInsert) {
        // Do This
    }
    else if(Trigger.isUpdate) {
        // Do That
    }
}

You can see how we checked the Trigger contexts using the context variable Trigger.isInsert & Trigger.isUpdate and decided on the next actions appropriately.

Suppose in the same Trigger you have used the below code :

trigger ContactTrigger on Contact (before insert, before update) {

    if(Trigger.isInsert) {
        // Do This for insert operation
    }
    else if(Trigger.isUpdate) {
        // Do That for update operation
    } 
    else if(Trigger.isDelete){
        // This block will never get executed as this trigger run -
        // only for before insert & before update operations
    }
}

Trigger.isDelete block would never get executed in the above code because the Contact Trigger is declared to be used before insert & before update operations.

Before diving into the Trigger Ocean, make yourself familiar with Apex Query Language – SOQL & SOSL.

Thank you for visiting SalesforceBlue.com
If you have any queries feel free to write down a comment below 🙂


Leave a Reply

Your email address will not be published. Required fields are marked *