Apex Triggers

Learn About Apex Triggers in Salesforce & When to Use It

Apex Triggers

Apex triggers allow custom actions to be performed before or after data manipulation language (DML) events occur in Salesforce, such as insertions, updates, and deletions. 

This concept automates a process and can be used to solve complex scenarios that are not possible to accomplish using workflow and process builder. Triggers can be used with DML operations as well. 

It is important to understand that there are two types of triggers:

Before — They are fired before inserting the record in the database.  

After - They are fired after inserting the record in the database. 

Trigger Events in Salesforce 

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

dont miss out iconDon't forget to check out: Crash Course on Apex Triggers Salesforce | Complete Guide with Real Time Scenarios 

Context variables in triggers — These contain all the records that were inserted in triggers that fired as a result of inserting or updating records. For example,  

Trigger.New contains all records that were inserted in triggers that fired as a result of inserting or updating records. As an update trigger, Trigger.Old provides the old version of sObjects that were changed, or a list of deleted sObjects that were deleted. Triggers can be invoked when a new record is added, or when a large number of records are inserted in bulk via API or Apex.   

The context variable Trigger.New is a context variable that can contain any number of records. The sObjects of Trigger.New can be retrieved individually by iteration over Trigger.New. 

isExecuting: An execution of Apex code is considered to be true if it is in the context of a trigger, rather than a web page, Web service, or executeanonymous() API call. 

isInsert: The trigger will be fired if an insert operation was performed, whether through the Salesforce user interface, Apex, or API. 

isUpdate: It is true if the trigger was fired by a Salesforce user interface, Apex, or API update. 

isDelete: A trigger that has been fired as the result of a delete operation, whether it came from Salesforce's user interface, Apex, or API. 

isBefore: Return true if this trigger was fired before any record was saved. 

isAfter: Return true if this trigger was fired after all records were saved. 

isUndelete:  The action is fired if the record has been recovered from the Recycle Bin (after it has been undeleted using the Salesforce user interface, Apex, or API.) 

new:  In insert, update, and undelete triggers, this sObject list shows the new versions of the records. The records can only be updated in before triggers. 

newMap:  In triggers that execute before the update, after the insert, after the update, and after the undelete, the IDs are mapped to the new versions of the sObject records. 

old:  Only available in triggers that update or delete records, this list returns the old version of the records. 

oldMap:  This map corresponds to the old versions of sObject records. It is available only in triggers for updating or deleting records. 

size: Total number of records in a trigger invocation, both old and new. 

dont miss out iconCheck out another amazing blog by Mohit here: Named Credentials as Callout Endpoints - Salesforce Developer Guide 

When to Use Salesforce Triggers 

In certain scenarios, triggers are the best option over point-and-click tools such as workflows. Workflow is a point-and-click tool that does not require any coding on the part of the user. The Workflow rule can be used for actions (emails, tasks, field updates or outbound messages) for the same object or from the child object to parent object. Trigger is an advanced version of Workflow that uses a programmatic approach (insert, update, merge, delete). 

Trigger works across all the objects. They can be used to create new records which is not possible with workflows. In addition, triggers work neither before nor after actions, while workflows work only after certain actions. 

Trigger Syntax 

trigger TriggerName on ObjectName (trigger_events) { 



Popular Salesforce Blogs