Introduction to Apex Trigger in Salesforce?

In Salesforce, ApexTrigger is a piece of code that is used to execute custom business logic before or after certain events occur on records in a Salesforce database.

The previous blog post was on the interface in Salesforce.

Table of Contents

Introduction

In Salesforce, ApexTrigger is a piece of code that is used to execute custom business logic before or after certain events occur on records in a Salesforce database.

An ApexTrigger is associated with a specific object, such as an Account or Contact, and is triggered by events like record creation, deletion, or updating. When the trigger is executed, it can perform a variety of actions, such as updating related records, sending emails, or performing calculations.

Syntax of Apex Trigger


					
				

Where

1. TriggerName – The Name of the ApexTrigger
2. ObjectApiName – The API Name of the Object
3. TriggerEvents – The Trigger events could be any of the following(before, after)

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

Example Apex Trigger


					
				

Event Type of the Apex Trigger

There are two types of Apex Trigger Events in Salesforce

  • before
  • after

Trigger Events in Salesforce

There are multiple apex trigger events for both before & after context

DML

  • insert
    • before
    • after
  • update
    • before
    • after
  • delete
    • before
    • after
  • undelete (insert =~ after insert )
    • after

– before insert
– after insert
– before update
– after update
– after undelete
– before delete
– after undelete

When to use before vs after trigger


Context Variables of Apex Triggers

Apex Triggers have access to several context variables that provide information about the trigger event and the records involved. These context variables are automatically provided by Salesforce and can be used within the trigger code to perform various operations.

These context variables provide a lot of information about the trigger event and can be used to perform various operations, such as updating related records, sending emails, or performing calculations.

Here are some of the most commonly used context variables in Apex Triggers:

  • Trigger.new: A list of the new versions of the sObject records that have been inserted or updated. This variable is only available in “before” triggers.
  • Trigger.old: A list of the old versions of the sObject records before they were updated. This variable is only available in “after” triggers.
  • Trigger.newMap: A map of the new versions of the sObject records, keyed by their record IDs.
    • Map<Id, sObject>
  • Trigger.oldMap: A map of the old versions of the sObject records before they were updated, keyed by their record IDs.
    • Map<Id, sObject>
  • Trigger.isInsert: A Boolean value indicating whether the trigger was fired due to an insert event.
  • Trigger.isUpdate: A Boolean value indicating whether the trigger was fired due to an update event.
  • Trigger.isDelete: A Boolean value indicating whether the trigger was fired due to a delete event.
  • Trigger.isUnDelete: A Boolean value indicating whether the trigger was fired due to a undelete event.
  • Trigger.isBefore: A Boolean value indicating whether the trigger was fired before the data was saved to the database.
  • Trigger.isAfter: A Boolean value indicating whether the trigger was fired after the data was saved to the database.
  • Trigger.size: An integer value indicating the number of records involved in the trigger event. Max – 200
  • Trigger.operationType – The current operation of the Apex Trigger can be identified by obtaining an enumeration of type System.TriggerOperation. Possible Values are – BEFORE_INSERT, BEFORE_UPDATE, BEFORE_DELETE,AFTER_INSERT, AFTER_UPDATE, AFTER_DELETE, and AFTER_UNDELETE.
  • Trigger.isExecuting – To determine if the current context for the Apex code is a trigger and not a Visualforce page, Web service, or execute anonymous API call, check if the returned value is true.

					
				

When a Context Variable is available in the Apex Trigger Events


Watch Complete Video

Hands-On


					
				

Resources

Amit Singh
Amit Singh

Amit Singh aka @sfdcpanther/pantherschools, a Salesforce Technical Architect, Consultant with over 8+ years of experience in Salesforce technology. 21x Certified. Blogger, Speaker, and Instructor. DevSecOps Champion

Articles: 299

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

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

Discover more from Panther Schools

Subscribe now to keep reading and get access to the full archive.

Continue reading