Apex Triggers Unit

Apex Triggers Unit in Salesforce - The Trailhead Guide

Apex Triggers  

Triggers are used to perform custom actions that are executed when a specific event takes place, it can be either before or after DML events. Triggers allow us to run complex custom actions that can’t be run using point-and-click tools in Salesforce like insertion, updates, or deletions. For managing records apex invokes trigger support when required. 

Triggers can be defined for standard objects like accounts, contacts, leads or custom objects. Triggers are active by default as soon as they're created. The trigger gets fired as soon as the specific event occurs. 

Trigger Syntax

trigger TriggerName on ObjectName (trigger_events) { 
    Code_block 
}

Here, the TriggerName is the name of the trigger, Object Name is the name of the object that the trigger is for and trigger events are the type of event/operation that fires the trigger. 

Types of Triggers

There are two kinds of triggers: 

  • Before trigger are used to update or validate records before a record is inserted or updated or deleted. 
  • After triggers are fired after the execution of an event. They are also used to access field values set by the system and affect the changes. The records that fire these triggers are read-only. 

dont miss out iconDon't forget to check out: Apex Design Patterns: The Singleton Pattern in Salesforce

Trigger Events

Triggers are fired when one or the subsequent events occur: 

  • Before insert  
  • Before update  
  • Before delete  
  • After insert  
  • After update  
  • After delete 
  • After undelete 

Context Variables

List of context variables in Salesforce   

  1. IsInsert – returns true if the trigger was fired due to an insert operation  
  1. IsUpdate – returns true if the trigger was fired because of update operation  
  1. IsDelete – returns true if trigger was fired because of a delete operation from the Salesforce user interface, API or Apex  
  1. IsBefore – returns true If trigger was fired before any record was saved  
  1. IsAfter – Returns true if the trigger fired after all record was saved  
  1. IsUndelete – returns true if this trigger was fired after an undelete operation or record is recovered from Recycle Bin 
  1. New- returns a list of new versions of the sObject records. Only limited to insert, update and undelete triggers. 
  1. Old- returns the list of old versions of the sObject records. Only available in update and delete triggers. 
  1. NewMap – A map of Ids of new versions of sObject records. Only limited to before update, after insert, after update and after undelete triggers. 
  1. Size – it is the total number of trigger invocations both old as well as new. 

Bulkified Trigger 

Trigger should be able to handle single records as well as thousand records. This can be achieved in the following way:

  • Writing triggers that operate on the collection of sObject record 
  • Writing triggers that perform SOQL and DML operations 

dont miss out iconCheck out another amazing blog by Ashutosh here: Overview of Aura Components in Salesforce | The Developer Guide

Implementation Considerations

  • Merge trigger fires on delete both events  
  • Upsert triggers fired on events of before insert and before the update and after insert and after update  
  • Field history isn’t recorded until the end of the trigger 
  • Callouts are made asynchronously so the trigger does not wait for a response  
  • If a trigger is completed successfully the changes are saved to the database and if it fails then the transaction is rolled back  

Responses

Popular Salesforce Blogs