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.  

Job Support Program

Online Work Support for your on-job roles.

jobservice

Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:

  • Pay Per Hour
  • Pay Per Week
  • Monthly
Learn MoreGet Job Support
Course Schedule
NameDates
Salesforce TrainingNov 23 to Dec 08View Details
Salesforce TrainingNov 26 to Dec 11View Details
Salesforce TrainingNov 30 to Dec 15View Details
Salesforce TrainingDec 03 to Dec 18View Details
Last updated: 28 Sep 2024
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