Home  >  Blog  >   Salesforce  > 

Context Variables In Salesforce

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

Rating: 5
  
 
9077
  1. Share:
Salesforce Articles

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:

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 (that is after an undelete operation from the Salesforce user interface, Apex, or theAPI.)
new Returns 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.
newMap A 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.
old Returns a list of the older versions of the sObjec records. Note that this sObject list is only available in update and delete triggers.
oldMap A map of IDs to the old versions of the sObject records. Note that this map is only available in update and delete triggers.
size The 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.   
Join our newsletter
inbox

Stay updated with our newsletter, packed with Tutorials, Interview Questions, How-to's, Tips & Tricks, Latest Trends & Updates, and more ➤ Straight to your inbox!

Course Schedule
NameDates
Salesforce TrainingAug 05 to Aug 20
Salesforce TrainingAug 08 to Aug 23
Salesforce TrainingAug 12 to Aug 27
Salesforce TrainingAug 15 to Aug 30
Last updated: 04 August 2023
About Author
Remy Sharp
Arogyalokesh

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.

Recommended Courses

1 /15