APEX Triggers

Triggers is an apex code which FIRES Whenever a DML Operation occurs

Syntax

Trigger on (events){
}

Events

There 2 types of Events

  1. Before events
  2. After Events

Before Events: Fires before a record gets saved in the DATABASE

Before insert
Before Update
Before Delete

After Events: Fires after a record has been saved on DATABASE
After insert
After Update
After Delete
After Undelete

Simple Trigger

[php] trigger Hello on Account (before insert) {
System.debug(‘Trigger Fired’);
}[/php]

With All EVENTS

[php]
trigger Hello on Account (before insert, after insert,before update,after update, before delete,after delete) { System.debug(‘Trigger Fired’);[/php]

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

This Context Variables return a boolean

Variable Usage
isExecuting Returns true if the current context for the Apex code is a trigger, not a Visualforce page, a Web service, or an executeanonymous() API call.
isInsert Returns true if this trigger was fired due to an insert operation, from the Salesforce user interface, Apex, or the API.
isUpdate Returns true if this trigger was fired due to an update operation, from the Salesforce user interface, Apex, or the API.
isDelete Returns true if this trigger was fired due to a delete operation, from the Salesforce user interface, Apex, or the API.
isBefore Returns true if this trigger was fired before any record was saved.
isAfter Returns true if this trigger was fired after all records were saved.
isUndelete Returns true if this trigger was fired after a record is recovered from the Recycle Bin. This recovery can occur after an undelete operation from the Salesforce user interface, Apex, or the API.
new Returns a list of the new versions of the sObject records.

This sObject list is only available in insertupdate, and undelete triggers, and the records can only be modified in before triggers.

newMap A map of IDs to the new versions of the sObject records.

This map is only available in before updateafter insertafter update, and after undelete triggers.

old Returns a list of the old versions of the sObject records.

This sObject list is only available in update and delete triggers.

oldMap A map of IDs to the old versions of the sObject records.

This map is only available in update and delete triggers.

operationType Returns an enum of type System.TriggerOperation corresponding to the current operation.

Possible values of the System.TriggerOperation enum are: BEFORE_INSERTBEFORE_UPDATEBEFORE_DELETE,AFTER_INSERTAFTER_UPDATEAFTER_DELETE, and AFTER_UNDELETE. If you vary your programming logic based on different trigger types, consider using the switch statement with different permutations of unique trigger execution enum states.

size The total number of records in a trigger invocation, both old and new.

[php]trigger Hello on Account (before insert, after insert,before update,after update, before delete,after delete) {

if(trigger.isbefore && trigger.isinsert){
System.debug(‘Trigger Fired due to before insert ‘);
}
if(trigger.isbefore && trigger.isupdate){
System.debug(‘Trigger Fired due to before Update ‘);
}
if(trigger.isbefore && trigger.isdelete){
System.debug(‘Trigger Fired due to before Delete ‘);
}

}[/php]