Context Variables In Salesforce

What exactly are context variables? This blog addresses many context variables and their corresponding explanations.

Table of Content - Context Variables In Salesforce

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

For Example: 

Trigger PropertyDiscountTrigger on Property__c (Before Insert){
    RowHouseDiscount.applyDiscount(Trigger.new);
}

Trigger. new - It is a default function in Salesforce. After trigger activation, it loads all the records. It cannot be modified, and trigger. new is called “Context Variable”. It is of List Type.

Get ahead in your career by learning Salesforce through Mindmajix Salesforce Online Training Course.

Trigger.new - Assume that it is going to give us lists of records due to which trigger got called.

What are Context Variables?

This is a special variable, managed/created by Salesforce itself, where we get the information about the Trigger. And that helps us to write better code with better practice.

We have different context variables, and they are mentioned below:

isExecutingReturns true if the current context for the Apex code is a trigger, not a Visualforce page, a Web service, or an executeanonymous() API call.
isInsertReturns true if this trigger was fired due to an insert operation, from the Salesforce user interface, Apex, or the API.
isUpdateReturns true if this trigger was fired due to an update operation, from the Salesforce user interface, Apex, or the API.
isDeleteReturns true if this trigger was fired due to a delete operation, from the Salesforce user interface, Apex, or the API.
isBeforeReturns true if this trigger was fired before any record was saved.
isAfterReturns true if this trigger was fired after all records were saved.
isUndeleteReturns true if this trigger was fired after a record is recovered from the Recycle Bin (that is after an undelete operation from the Salesforce user interface, Apex, or theAPI.)
newReturns a list of the new versions of the sObject records.Note that this sObject list is only available in insert and update triggers, and the records can only be modified before triggers.
newMapA map of IDs to the new versions of the sObject records. Note that this map is only available before the update, after insert, and after update triggers.
oldReturns a list of the older versions of the sObjec records. Note that this sObject list is only available in update and delete triggers.
oldMapA map of IDs to the old versions of the sObject records. Note that this map is only available in update and delete triggers.
sizeThe total number of records in a trigger invocation, both old and new.

Sample Code for isExecuting Skeletal Structure of program with Logic

 MindMajix YouTube Channel

Trigger in Apex Class:

---Code---
---Code---

if(Trigger.isExecuting == True){

    ---Code---
}

---Code---
---Code---
---Code---

Sample Code for “isInsert”: Skeletal Structure of program with Logic

Trigger in Apex Class:

Program Logic#1:

---Code---
---Code---

if(Trigger.isInsert == True){

    ---Code---
}

---Code---
---Code---
---Code---

Program Logic#2: Skeletal Structure of program with Logic

---Code---
---Code---
if(Trigger.isInsert == True && Trigger.isUpdate == True ){
    ---Code to work only before insert is done----
    ---Not for update, not for insert-------------------
}

---Code---
---Code---
---Code---

…..Same way :: we can process for

isUpdate
isDelete
isBefore
isAfter
isUndelete

Program Logic#3: How to call a code in the trigger which client wants to call when an update is done.

Solution: Skeletal Structure of program with Logic

---Code---
---Code---

if(Trigger.isUpdate == True && Trigger.isAfter == True ){

    ---Code to work only before insert is done----
    ---Not for update, not for insert-------------------
}

---Code---
---Code---
---Code---

Solution: Skeletal Structure of program with Logic

---Code---
---Code---
if(Trigger.isBefore == True && Trigger.isDelete == True ){

    ---Code to work only before insert is done----
    ---Not for update, not for insert-------------------
}
---Code---
---Code---
---Code---

In the next topic, we will discuss in-detail about “Context Variables in Salesforce - 1”. Keep following us for more info on Salesforce Development / Programming.

Explore Salesforce Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!

Mindmajix offers different Salesforce certification training according to your desire with hands-on experience on Salesforce concepts

Salesforce Administration Training Salesforce Lightning Training 
Salesforce Advanced Developer Training Salesforce Developer Training 
Salesforce IoT Training Salesforce App Builder Certification Training 
Salesforce AppExchange Training Salesforce Service Cloud Training 
and many more.  
Course Schedule
NameDates
Salesforce TrainingMay 28 to Jun 12View Details
Salesforce TrainingJun 01 to Jun 16View Details
Salesforce TrainingJun 04 to Jun 19View Details
Salesforce TrainingJun 08 to Jun 23View Details
Last updated: 31 Jul 2023
About Author

Arogyalokesh is a Technical Content Writer and manages content creation on various IT platforms at Mindmajix. He is dedicated to creating useful and engaging content on Salesforce, Blockchain, Docker, SQL Server, Tangle, Jira, and few other technologies. Get in touch with him on LinkedIn and Twitter.

read less
  1. Share:
Salesforce Articles