The Role of Trigger Context Variables in Apex Triggers


28/03/2024


After completing this unit, you will be able to:

  • What are trigger context variables?
  • Different types of trigger context variables

 

What are trigger context variables?

Trigger context variables in Salesforce Apex are collections of data that provide specific information about the context in which a trigger is executed. They help the trigger know things like what kind of change was made, which records were involved, and what the data looked like before and after the change. These variables are crucial for the trigger to perform the right actions based on what's happening in the database.

 

Different types of trigger context variables: 

  1. Trigger.New: This variable provides a list of new records when the trigger event is before insert, after insert, before update, after update, and after undelete.

    Example: 

    trigger trigger_name on Account (before insert, before update) {
        for (Account account : Trigger.new) {
            if (account.Name.contains('Test')) {
                account.Description = 'This is a test account';
            }
        }
    }
     
  2. Trigger.Old: This variable provides a list of old records when the trigger event is before update, after update, before delete, and after delete.

    Example:

    trigger trigger_name on Opportunity (before update) {
        for(Opportunity oldOpportunity : Trigger.old) {
            Opportunity newOpportunity = Trigger.newMap.get(oldOpportunity.Id);
            if(newOpportunity.Amount != oldOpportunity.Amount) {
                System.debug('Opportunity Amount changed from ' + oldOpportunity.Amount + ' to ' + newOpportunity.Amount);
            }
        }
    }

     

  3. Trigger.newMap: This variable provides a list of new records indexed by their IDs when the trigger event is after insert, before update, after update, and after undelete.

    Example:

    trigger trigger_name on Opportunity (before update) {
        Map<Id, Opportunity> newOpportunitiesMap = new Map<Id, Opportunity>(Trigger.newMap);
        for (Id opportunityId : newOpportunitiesMap.keySet()) {
            Opportunity newOpportunity = newOpportunitiesMap.get(opportunityId);
            if (newOpportunity.Amount > 10000) {
                newOpportunity.StageName = 'High Value';
            }
        }
    }

     

  4. Trigger.oldMap: This variable provides a list of old records indexed by their IDs when the trigger event is before update, after update, before delete, and after delete.

    Example:

    trigger trigger_name on Opportunity (before update) {
        for (Opportunity newOpportunity : Trigger.new) {
            Opportunity oldOpportunity = Trigger.oldMap.get(newOpportunity.Id);
            if (newOpportunity.Amount != oldOpportunity.Amount) {
                newOpportunity.Amount.addError('Amount cannot be changed);
            }
        }
    }

     

  5. Trigger.isInsert: This is a boolean variable that returns true when the trigger is fired due to an insert operation.

    Example:

    trigger trigger_name on Account (before insert, before update) {
        if (Trigger.isInsert) {
            System.debug('Trigger fired due to an insert operation.');
        } else {
            System.debug('Trigger fired due to an operation other than insert.');
        }
    }

      

  6. Trigger.isUpdate: This is a boolean variable that returns true when the trigger is fired due to an update operation.

    Example:

    trigger trigger_name on Account (before insert, before update) {
        if (Trigger.isUpdate) {
            System.debug('Trigger fired due to an update operation.');
        } else {
            System.debug('Trigger fired due to an operation other than update.');
        }
    }

     

  7. Trigger.isDelete: This is a boolean variable that returns true when the trigger is fired due to a delete operation.

    Example:

    trigger trigger_name on Account (before delete) {
        if (Trigger.isDelete) {
            System.debug('Trigger fired due to a delete operation.');
        } else {
            System.debug('Trigger fired due to an operation other than delete.');
        }
    }

      

  8. Trigger.isUndelete: This is a boolean variable that returns true when the trigger is fired after a record is recovered from the Recycle Bin.

    Example:

    trigger trigger_name on Account (after undelete) {
        if (Trigger.isUndelete) {
            System.debug('Trigger fired due to an undelete operation.');
        } else {
            System.debug('Trigger fired due to an operation other than undelete.');
        }
    }

      

  9. Trigger.isBefore: This is a boolean variable that returns true when the trigger is fired before any record is saved.

    Example:

    trigger trigger_name on Account (before insert, before update) {
        if (Trigger.isBefore) {
            System.debug('Trigger fired before any record is saved.');
        } else {
            System.debug('Trigger fired after record saving.');
        }
    }

      

  10. Trigger.isAfter: This is a boolean variable that returns true when the trigger is fired after all records are saved.

    Example:

    trigger trigger_name on Account (after insert, after update) {
        if (Trigger.isAfter) {
            System.debug('Trigger fired after all records are saved.');
        } else {
            System.debug('Trigger fired before all records are saved.');
        }
    }

      

  11. Trigger.isExecuting: This is a boolean variable that returns true if the code is running within a trigger context, otherwise false.

    Example:

    trigger trigger_name on Account (before insert, before update) {
        if (Trigger.isExecuting) {
            System.debug('Code is executing within a trigger context.');
        } else {
            System.debug('Code is not executing within a trigger context.');
        }
    }