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){ //Code_block }
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
- In the developer console, go to File then New then click Apex Trigger.
- Enter any name for your trigger example HelloWorld, then select Account for the sObject. Click Submit.
- Write your code. For example:
Trigger HelloWorld on Account(before insert){ system.debug(‘Hello World!’); }
- To save, press Ctrl+S.
- To test a trigger we have to create an account.
- Click Debug then Open Execute Anonymous Window.
- In the new window, add the following and then click Execute.
Account a = new Account (Name = ‘Test Trigger’); Insert a;
Using Trigger Context Variables
- IsExecuting – It returns true is the current context of the apex code is trigger.
- IsInsert – It returns true if trigger is fire due to insert operation.
- IsUpdate – It returns true if trigger is fire due to update operation.
- IsDelete – It returns true if trigger is fire due to delete operation.
- IsBefore – It returns true if trigger is fire before saving any record.
- IsAfter – It returns true if trigger is fire after saving any record.
- IsUndelete – It returns true if trigger is fire after a record is recovered from the recycle Bin.
- New – It returns a list of new versions of sObject records.
- Old – It returns a list of old versions of sObject records.
Check 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.