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.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.

No comments:

Post a Comment