1. We can perform custom operations before or after events to records.

  2. Use triggers to do operations that cannot be done by point-and-click tools provided in Salesforce.

  3. We can do things using triggers that we can do in Apex, including SOQL and DML or calling custom methods.

  4. Triggers can be created for both Standard and Custom Objects.

  5. By default, triggers are active as you create them.

  6. Types of Triggers

    1. Before Triggers:
      1. It is used to update or validate record values before saved to database.
    2. After Triggers:
      1. It is used to access field values that are set by the system such as Ids, and to make changes in the other records. The records that fire the after trigger are read-only.
  7. Syntax :

    trigger trigger_name on Object_Name ( trigger_events ) {

    // block of code

    }

  8. Trigger events :

    1. before insert
    2. before update
    3. before delete
    4. after insert
    5. after update
    6. after delete
    7. after undelete
  9. Example of Trigger :

    image.png

  10. It is best practice to create only one trigger for one object.

    1. Trigger Context Variables

      All triggers define implicit variables that allow the developer to access run-time context. These variables are contained in the System.Trigger class.

      1. isExecuting
        • Returns true if the current context of Apex code is a trigger, not a VF page, a web service, or an executeanonymous() API Call.
      2. isInsert
        • Returns true if trigger was fired due to an insert operation, from the Salesforce UI, Apex or API.
      3. isUpdate
        • Returns true if trigger was fired due to an update operation, from the Salesforce It, Apex or API.
      4. isDelete
        • Returns true if trigger was fired due to delete operation, from the Salesforce UI, Apex or API.
      5. isBefore
        • Returns true of the trigger was fired before any record was saved.
      6. isAfter
        • Returns true if the trigger was fired after all records were saved.
      7. isUndelete
        • Returns true if the trigger was fired after a record is recovered from Recycle Bin
      8. size
        • The total number of records in a trigger invocation, both old and new.
      9. new
        • Returns a list of new versions of sObject records.
        • This sObject list is available in insert, update and undelete triggers and then record can only be modified in before trigger.
      10. newMap
        • A Map of ids to the new versions of sObject records.
        • Available in after insert, before update, after update, after undelete triggers.
      11. old
        • Returns a list of old versions of sObject records.
        • Available in before update, after update, before delete, after delete triggers.
      12. oldMap
        • A map of ids to the old versions of sObject records.
        • Available in before update, after update, before delete, after delete triggers.
  11. In the case of Trigger.Before events, we don’t need to fire any DML Operations.

  12. Call Apex Method through Trigger :

image.png