Apex Triggers in Salesforce Here’s What you Need to Know

Apex Triggers in Salesforce | Here’s What you Need to Know

What are Apex triggers?

Apex triggers permit custom moves to be done before or after data manipulation language (DML) activities arise in Salesforce, such as insertions, updates, and deletions.

This concept automates a procedure and may be used to clear up complex situations that are not feasible to perform using workflow and system builder. Triggers may be used with DML operations as properly.

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

  1. Before: These are fired before inserting the record in the database.
  2. After: These are fired after inserting the record in the database.

The various trigger events in salesforce are:

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

dont miss out iconDon't forget to check out: Deleting Apex Classes / Apex Triggers From Production Using Workbench | Salesforce Tutorial

What are Context Variables in Triggers?

These include all the information that have been inserted in triggers that fired because of placing or updating records. As an example:

Trigger.New includes all records that have been inserted in triggers that fired due to inserting or updating records. As an update trigger, Trigger.old gives the old version of sObjects that had been modified, or a listing of deleted sObjects that were deleted.

Triggers can be invoked when a new record is added, or when a massive quantity of records are inserted via API or Apex. The context variable trigger.New is a context variable which could incorporate any quantity of records. The sObjects of trigger.New may be retrieved in my opinion by using iteration over Trigger.New.

Let's understand the various triggers and related terms now:

  • isExecuting: An execution of Apex code is taken into consideration to be authentic if it's miles in the context of a trigger, instead of a web page, net service, or executeanonymous() API name.
  • isInsert: The trigger will be fired if an insert operation was performed, whether theough 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: 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:  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 indicates the new variations 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 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 variations of sObject records. it's available only in triggers for updating or deleting records.
  • Size: The total number of records in a trigger invocation, both old and new.

dont miss out iconCheck out another amazing blog by Nikhil here: What are Flow Best Practices in 2023?

When to Use Salesforce Triggers?

In positive situations, triggers are the first-class alternative over point-and-click on tools such as workflows. Workflow is a point-and-click device that does not require any coding on the part of the person. The Workflow rule can be used for actions (emails, tasks, field updates or outbound messages) for the identical item or from child object to parent object. Trigger is a advanced model of Workflow that uses a programmatic method (insert, update, merge, delete).

Trigger works throughout all of the objects. They can be used to create new statistics which isn't always 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