Monday, October 28, 2019

WHAT ARE TRIGGER CONTEXT VARIABLES IN SALESFORCE?


Trigger context variables play a very important role in TRIGGER execution, context variables are required when we want a piece of code inside trigger to runs only for a particular event. Using context variable we can specify which piece of code we want to run for which particular event avoiding running of other pieces of code which are not required to be run for the specific event.
For example, if I want my piece of code inside TRIGGER to run only for BEFORE INSERT event I can make use of the context variable as shown below:

if(trigger.isbefore && trigger.isinsert)
    {
     // Code Block
    }

Now let see what are different context variables we deal with while we work with TRIGGERS,

trigger.isafter()
=>Check if the trigger is running in after mode, If the trigger is running in after mode it will return true and allow the block of code to execute.

trigger.isbefore()
=>Check if the trigger is running in before mode, If the trigger is running in before mode it will return true and allow the block of code to execute.

trigger.isupdate()
Check if the trigger is running in update mode, If the trigger is running in update mode it will return true and allow the block of code to execute.

trigger.isdelete()
Check if the trigger is running in delete mode, If the trigger is running in delete mode it will return true and allow the block of code to execute.

trigger.isinsert()
Check if the trigger is running in insert mode, If the trigger is running in insert mode it will return true and allow the block of code to execute.

trigger.isundelete()
Check if the trigger is running in undelete mode, If the trigger is running in undelete mode it will return true and allow the block of code to execute.

trigger.isexecuting()
Check if the apex class method is getting called from apex trigger, If getting called return true and allow the apex class method to run.

trigger.new()
Stores the new list of records.

trigger.newmap()
Stores new records with id.

trigger.old()
Stores the old list of records.

trigger.oldmap()
Stores old records with id.

operationType

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

Possible values of the System.TriggerOperation enum are: BEFORE_INSERT, BEFORE_UPDATE, BEFORE_DELETE,AFTER_INSERT, AFTER_UPDATE, AFTER_DELETE, and AFTER_UNDELETE. 

size

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

3 comments: