Triggers:
A trigger is a block of Apex Code or Function that executes when DML Operations occur. DML Operations:
1. Insert
2. Update
3. Delete
4. Undelete
Types of Triggers: There are two types of Triggers
1. Before Triggers
2. After Triggers Before Triggers:
Before Triggers: These are for the purpose of Data Validation or Data Manipulation. Before Triggers can also be used to prevent a DML Operation on a record from occurring using the addError Method on a record.
After Triggers: Used when you need to access record IDs or change related records after the record is saved.
Events: There are two types of events in triggers Before Event After Event Trigger executes in the following Events:
1. Before Insert
2. After Insert
3. Before Update
4. After Update
5. Before Delete
6. After Delete
7. After Undelete
Note: There is no event before the Undelete operation.
Context Variables: These are the implicit variables defined by the trigger which helps to access run time contexts such as the type of triggers and the list of records the trigger operates on.
Records that caused the trigger to fire can be accessed and modified using these context variables such as Trigger.new, Trigger.old, Trigger.OldMap and Trigger.newMap.
Also some other context variables return a Boolean value to determine whether the trigger was fired due to insert, update or some other Operation and to define whether it is before or after the trigger.
Trigger Best Practices:
1. Bulkify your code
2. Avoid writing SOQL and DML Operations inside the loop.
3. Maintain Logic Less Triggers (Using Handler and helper Apex classes)
4. Use collection to store data
Avoid Hardcoding Trigger Syntax: Defined by the keyword ‘trigger’ followed by its name on the salesforce object associated with the events. Trigger Trigger_Name on sObject (TriggerEvents)
{ }
Example: Trigger OpportunityTrigger on Opportunity (before insert, after insert, before update, after update, before delete, after delete, after undelete)
{ // Logic }

"Apex is a powerful and versatile tool that really sets Salesforce apart when it comes to customizing and automating processes within the platform. With its strong integration capabilities, developers can create seamless business logic, build custom applications, and tailor Salesforce to meet any organization's unique needs. Apex’s ease of use, combined with its robust features, makes it a vital skill for any Salesforce developer looking to truly leverage the platform's potential."
ReplyDelete