Apex Triggers in Salesforce | Here’s the Ultimate Salesforce Guide

Apex Trigger

 Trigger is a concept of automating processes and solving complex scenarios. These are stored procedures which execute whenever any particular event occurs. We can perform custom actions before or after changes to Salesforce records. 

For example, they are used to perform operations such as modifying related records or restricting certain operations. They are also used to execute SOQL and DML or call the custom apex method. 

These are used to perform tasks which can’t be done by point-and-click tools in the Salesforce user interface. Triggers are active by default which means Salesforce automatically fires active triggers when the specified database events occurs. 

Trigger Syntax

trigger TriggerName on ObjectName(trigger_events){ 

Apex triggers are of two types: – before and after which will fire before or after inserting the data into the database respectively. 

  • Before Trigger- It is used to update or validate record values before we save them into the database.
  • After Trigger– It is used to access field values which are set by the system and to affect the changes in other records. Those records are read-only. 

A Trigger executes the following operations:- 

Don’t forget to check out: Here’s All you need to Know About Asynchronous Apex in Salesforce

Steps to Create a Trigger

  1. In the developer console, go to File then New then click Apex Trigger. 
  2. Enter any name for your trigger example HelloWorld, then select Account for the sObject. Click Submit. 
  3. Write your code. For example:
    Trigger HelloWorld on Account(before insert){ 
        system.debug(‘Hello World!’); 
  4. To save, press Ctrl+S.
  5. To test a trigger we have to create an account.
  6. Click Debug then Open Execute Anonymous Window.
  7. In the new window, add the following and then click Execute.
    Account a = new Account (Name = ‘Test Trigger’); Insert a;

Using Trigger Context Variables

  1. IsExecuting – It returns true is the current context of the apex code is trigger. 
  2. IsInsert – It returns true if trigger is fire due to insert operation. 
  3. IsUpdate – It returns true if trigger is fire due to update operation. 
  4. IsDelete – It returns true if trigger is fire due to delete operation. 
  5. IsBefore – It returns true if trigger is fire before saving any record. 
  6. IsAfter – It returns true if trigger is fire after saving any record. 
  7. IsUndelete – It returns true if trigger is fire after a record is recovered from the recycle Bin. 
  8. New – It returns a list of new versions of sObject records. 
  9. Old – It returns a list of old versions of sObject records. 

dont miss out iconCheck out an amazing Salesforce video tutorial here: Apex Design Patterns: The Singleton Pattern in Salesforce

Bulk Trigger

All triggers are Bulk triggers by default and can process multiple records at a time, Bulk triggers can handle both single record update and bulk operations including: – 

  • Data Import 
  • Lightning Platform Bulk API calls 
  • Mass actions for record changes 
  • Recursive Apex methods and triggers which invoke Bulk DML statements 

Trigger Exceptions

Trigger can be used to prevent DML operations from occurring by calling the addError() method on a record or a field. When used on Trigger.new records in insert and update triggers, and on Trigger.old records in delete triggers, the custom error message is displayed in the application interface and logged. 


Source link

Interesting Posts

Leave a Reply

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