triggers in salesforce

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.

  1. Before Trigger: These triggers are fired before the new data is saved to the database.
  2. 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. 

  1. Before Insert 
  2. After Insert 
  3. Before update 
  4. After update
  5. Before Delete
  6. After Delete 
  7. After Undelete 

Trigger Context Variables

These variables are used to hold data required during the runtime of the operation

dont miss out iconDon'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: 

  1. Trigger.new: The list of new records that are trying to insert is stored in Trigger.new 
  2. In the before insert new records which we are trying to insert cannot be fetched using SOQL.
  1. If you want to modify the records which are in trigger.new, we can perform Write Operation.
  1. If you want to create child record object then use AFTER INSERT.

dont miss out iconCheck 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:

  1. Trigger.new: The list of new records which are SUCCESSFULLY SAVED to the   are in Trigger.new
  1. 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; 
} 

Popular Salesforce Blogs