Apex Trigger

What is the trigger?


  • A trigger is an Apex code that executes before or after the following types of operations: Insert, Delete, Update, Upsert.
  • Triggers are used to manipulate the data or put the validations on data.

Events of the triggers do have some prefixes:
  1. Before
  2. After

Syntax:

Trigger Trigger_Name on Object_Name(triggers Events)
{
        //Implement the logic here

}


Trigger Context Variables:-

  • new: Returns a list of the new version of sObject records.
  • old: Returns a list of the old version of sObject records.
  • isInsert: Returns true if the trigger was fired due to the insert operation.
  • isUpdate: Returns true if the trigger was fired due to the update operation.
  • isDelete: Returns true if the trigger was fired due to the delete operation.
  • isBefore: Returns true if the trigger was fired before a record is saved.
  • isAfter: Returns true if the trigger was fired after a record is saved.


Types of Triggers:-

  1. Before Triggers.
  2. After Triggers.
Before Triggers:
These triggers are fired before the data is saved into the database. Before triggers can modify values before they are saved to the database. 

For example:-
Change Account name (append 'Test') when a user creates or updates Account.

trigger ChangeNameOfAccount on Account(before insert, before update)
{
       for(Account a : Trigger.new)
       {
              a.Name = a.Name + 'Test';
       }
}

After Triggers:
These triggers are fired after the data is saved into the database.

For example:-
Change Account name (append 'Last') when a user creates an Account.

trigger addStrings on Account (after insert) 
{
    Set<String> setOfIds = new Set<String>(); //set of string to store id's of Account
    List<Account> accountList = new List<Account>(); //store list of Account
    for(Account a : Trigger.New)
    {
        setOfIds.add(a.id); // add id's of all Account
    }
    for(Account a : [Select id,name from Account where id IN : setOfIds])
    {
        a.Name = a.Name+' Last'; //append 'Last' in the Name of Account
        accountList.add(a); // add list of appended Account Name
    }


    Update accountList; //update list of Account Name

}



Difference between Before Triggers and After Triggers:-

Before Trigger After Trigger

1. We don't have an ID for the record.          1. We have an ID for the record.
2. We cannot use DML operation on             2. We can use DML operation on 
     these triggers.                                                these triggers.
3. Perform logic on the same object.            3. Perform logic on the related object.



How is trigger works?

COMMIT is the final stage of data insertion.
  1. If we want to create a record in Salesforce by clicking the Save button.
  2. After clicking Save button corresponding triggers is fired and check for validation/code.
  3. If all data of record are valid then it will COMMIT otherwise it will show error.
DATA (RECORD) -------→ SAVE -----→ VALIDATIONS/CODE/TRIGGERS ---→ COMMIT
 

Comments

Popular posts from this blog

AVL (Adelson-Velskii and Landis) Trees

Tree

How to Build REST API Using PHP