Triggers
As you learned in the previous section, Apex code can be used to write classes or triggers. A trigger is an Apex script that executes when a data manipulation language (DML) event occurs on a specific sObject. Therefore, triggers are directly bound with the records of a particular sObject. If the particular record is affected, then the related trigger will be executed:
The record is accessed via the standard page or Visualforce page
The record is accessed via a web services API
Triggers can be created on any custom or top-level standard object. There are four types of DML events that affect a trigger of a particular object:
Insert
Update
Delete
Undelete
The preceding DML events can be executed either before or after the event. You can specify that in the trigger defining:
before: Before triggers can update variable values before they are saved to the database.
after: After triggers can access field values that are set automatically (such as ID or last updated fields) or to affect changes...