Introduction To Trigger Syntax In Salesforce: A Beginner’s Guide

trigger TriggerName on ObjectName (trigger_events) {
   // code_block
}

TriggerName: The trigger’s name, such as AccountHandler. ObjectName: The object that initiates the trigger, like Account. trigger_events: This can be a list separated by commas, including one or more of the following events:

  • before insert
  • before update
  • before delete
  • after insert
  • after update
  • after delete
  • after undelete

For example, the following code defines a trigger for the before insert and before update events on the Account object:

trigger AccountHandler on Account (before insert, before update) {
    // Your code here
}

Context Variables (or contextual variables)

To access the records that triggered the execution of the trigger, utilize context variables. Trigger.New encompasses all the records inserted during insert or update triggers. Trigger.Old presents the previous versions of values before updates in update triggers or a roster of deleted values in delete triggers.

To handle bulk data operations, like those performed through Data Loader or integrations with other systems, it’s recommended to use a for loop to iterate over Trigger.New and access each individual sObject. Below is a sample trigger that updates the Description field when new accounts are created, either populating it if it’s empty or overwriting the existing value.

trigger AccountHandler on Account (before insert) {
    for (Account a : Trigger.New) {
        a.Description = 'Hello World';
    }   
}

a = variable of object Account

Example of an after insert trigger:

trigger AccountHandler on Account (after insert) {
    for (Account acc : Trigger.New) {
        Task t = new Task();
        t.Subject = 'Hello World';
        t.Priority = 'High';
        t.WhatId = acc.Id;
        insert t;   
    }   
}


Compare this trigger with the earlier before insert trigger. In this trigger, we will create a Task and associate it with the newly created Account, hence we require the Account Id.

Note: In the triggers mentioned earlier, we directly incorporate the logic into the trigger, which will function but is not considered best practice. Ideally, a trigger should only serve as a traffic controller, directing to a specific Apex class based on the event and context.

Here is another sample trigger using if and else syntax along with trigger context variables.

trigger AccountHandler on Account (before insert, after insert, after delete) {
    if (Trigger.isInsert) {
        if (Trigger.isBefore) {
            // Process before insert
        } else if (Trigger.isAfter) {
            // Process after insert
        }        
    }
    else if (Trigger.isDelete) {
        // Process after delete
    }
}

Context variables available for triggers include:

  • isExecuting: Returns true if the current context for the Apex code is a trigger, not a Visualforce page, a Web service, or an executeanonymous() API call.
  • isInsert: Returns true if this trigger was fired due to an insert operation, whether from the Salesforce user interface, Apex, or the API.
  • isUpdate: Returns true if this trigger was fired due to an update operation, whether from the Salesforce user interface, Apex, or the API.
  • isDelete: Returns true if this trigger was fired due to a delete operation, whether from the Salesforce user interface, Apex, or the API.
  • isBefore: Returns true if this trigger was fired before any record was saved.
  • isAfter: Returns true if this trigger was fired after all records were saved.
  • isUndelete: Returns true if this trigger was fired after a record is recovered from the Recycle Bin, i.e., after an undelete operation from the Salesforce user interface, Apex, or the API.
  • new: Returns a list of the new versions of the sObject records. Note that this sObject list is only available in insert and update triggers, and the records can only be modified in before triggers.
  • old: Returns a list of the old versions of the sObject records. Note that this sObject list is only available in update and delete triggers.