Get Started With Triggers in Salesforce | The Complete Guide
Triggers are the automated actions that are performed whenever DML operations are performed on the object.
- Before Trigger: These triggers are fired before the new data is saved to the database.
- After Trigger: These triggers are fired after the data is saved to the database.
Trigger Events
These are the events that will fire the triggers when DML is performed.
- Before Insert
- After Insert
- Before update
- After update
- Before Delete
- After Delete
- After Undelete
Trigger Context Variables
These variables are used to hold data required during the runtime of the operation.
Don't forget to check out: Apex Trigger In Salesforce - The Developer Guide
IsInsert : This will return true if the trigger is fired due to DML operation of insert.
IsUpdate : This will return true if the trigger is fired due to DML operation of update.
IsDelete : This will return true if the trigger is fired due to DML operation of delete.
IsUndelete : This will return true if the trigger is fired due to DML operation of undelete.
IsBefore: This will return true if the trigger is fired due to before event.
IsAfter: This will return true if the trigger is fired due to after event.
New: This will return a list of a new version of records on which DML is performed. NewMap: This will return map of new version of records with record id as key and record as value.
Old: This will return a list of the old version of records on which DML is performed. OldMap: This will return map of old version of records with record id as key and record as value.
Syntax:
Trigger TriggeName on ObjectName(trigger events) { }
INSERT TRIGGER
: Before Insert:
- Trigger.new: The list of new records that are trying to insert is stored in Trigger.new
- In the before insert new records which we are trying to insert cannot be fetched using SOQL.
- If you want to modify the records which are in trigger.new, we can perform Write Operation.
- If you want to create child record object then use AFTER INSERT.
Check out another amazing blog by Kirandeep here: What is Batch Apex in Salesforce? How to Execute Batch Apex Class?
Example1: Whenever new account is created with industry as banking then set ownership as private
Apex Trigger:
trigger ItriggeronAccount on Account (before insert) { for(account a:trigger.new) { if(a.industry=='Banking') { a.ownership='Private'; } } }
After Insert:
- Trigger.new: The list of new records which are SUCCESSFULLY SAVED to the are in Trigger.new
- After insert new records which are in trigger.new can be fetched using SOQL.
If you want to modify the records which are in trigger.new, we can perform DML operation.
Example1: Whenever new Account is created with Industry as EDUCATION and RATING as Warm. Then create new CHILD opportunity with Opportunity name as account name and other details to account
Apex Trigger: triggerAcc on Account (before insert) { list<opportunity> opty=new list<opportunity>(); for(account a:trigger.new) { if(a.Industry=='Education' &&a.Rating=='Warm'){ opportunity op=new opportunity(); op.name=a.name; op.Type='New Customer'; op.CloseDate=system.today()+15; op.StageName='Prospecting'; op.AccountId=a.Id; opty.add(op); } } insert opty; }