Hurry! 20% Off Ends SoonRegister Now

Context Variables

Context variables are implicit variables that allow developers to access run-time context. This post explains how to use a Trigger to perform a task.

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

 

 

Want to enhance your skills in dealing with the world's best CRM, enroll in our Salesforce Online Course.

What is Context Variable?

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.

 

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 as “Context Variable”. It is of List Type.

 
Trigger.new - Assume that it is going to give us lists of records due to which trigger got called.
 
 
Get ahead in your career by learning Salesforce through Mindmajix Salesforce Training.

 

Now let us compare Trigger.new Vs Trigger.old:

 

The latest version of that particular record will be inside the “Trigger.new”, Until the trigger gets called. The IMMEDIATE previous version of that particular record(values before modification) will be inside the “Trigger.old”, Until the trigger gets called.

 

Eg: “Employee Details”  -  Object in Salesforce.

 

Scenario: Using INSERT Command insert the Data of employee details - Harry | age-20 | Place - NY into the Object and observe the difference between Trigger.new & Trigger.old. 

 

 MindMajix YouTube Channel

If the trigger goes to Trigger.New ?

 

Solution: The value will be - (Trigger.New = Harry | 20 | NY)

 

If the trigger goes to Trigger.Old ?

 

Solution: The value will be - (Trigger.New = NA)

 

Program Scenario: Using UPDATE Command update the records of the employee details - ( Larry| age-20 | Place - NY &  Harry| age-20 | Place - NY) which are inserted, and observe the difference between Trigger.new & Trigger.old

 

Before Update the record values:

 

(Trigger.New = Larry | 20 | NY)
(Trigger.Old = Harry | 20 | NY)

 

Logic - Instead of “Harry”, Employee Name “Larry”  is updated.

 

After Update the record values:

 

(Trigger.New = Jerry | 20 | NY)
(Trigger.Old = Larry | 20 | NY)

 

If the trigger goes to Trigger.New ?

 

Solution: The value will be - (Trigger.New = Jerry | 20 | NY)

 

If the trigger goes to Trigger.Old ?

 

Solution: The value will be - (Trigger.Old = Larry | 20 | NY).

 

Note: Instead of the employee name - “Harry”, “Larry” is updated in the Trigger.old, because it is the IMMEDIATE Previous update in the entry. Trigger.old comes in, only when the record gets EDITED.

 

Trigger.new & Trigger.old” - gives the data of the current record in the form of “List Data Type”. 
Trigger.newMap & Trigger.oldMap” - gives the data of the current record in the “Map format”. It is the basic difference between New, Old & NewMap, OldMap.

 

Syntax For newMap, oldMap of Context Variables: The syntax for the content variable od oldMap & newMap are mentioned below:

 

Trigger.NewMap = (Map<id, entire="" record="">)
Trigger.OldMap = (Map<id, entire="" record="">)</id,></id,>

 

Eg: [ <(ATDF2GTY6789), (Larry | 20 | NY )> ]

 

Trigger.Size () -  It specifies the count of the records present in the Trigger.

 

Trigger Best Practice: We will discuss in detail with a scenario.

 

Program#1 for Context Variable Demo:

 

Public class ContextVariableDemo{
Public static void functionA(){
//code
//call this code only when record is about to get inserted
system.debug(‘Function A got called!’);
}
Public static void functionB(){
//code
//call this code only when record is about to get updated
system.debug(‘Function B got called!’);
}
Public static void functionC(){
//code
//call this code only when record is already inserted
system.debug(‘Function C got called!’);
}
Public static void functionD(){
//code
//call this code only when record is already updated
system.debug(‘Function D got called!’);
}
}

 

The Solution for the above scenario,

Trigger JeanDiscountTrigger on Levis__c (Before Insert, Before Update, After Insert, After Update){
if(trigger.isBefore==True && trigger.isInsert==True){
ContextVariableDemo.functionA();
}
if(trigger.isBefore==True && trigger.isInsert==True){
ContextVariableDemo.functionB();
}
if(trigger.isBefore==True && trigger.isInsert==True){
ContextVariableDemo.functionC();
}
if(trigger.isBefore==True && trigger.isInsert==True){
ContextVariableDemo.functionD();
}
}

 

Note: 

Never use any trigger without Context Var.
Always use one trigger/object.
Always write class of the code.

 

Frequently Asked Salesforce Interview Questions & Answers

 
 

Program#2 for Sample Wrong Code For Context Variable Trigger

 
=============Trigger===========

Trigger MobileDiscountTrigger on Mobile_c (after insert){
MobileClassDemonstration MyObj = New MobileClassDemonstration ();
MyObj.applyDiscount(Trigger.new);
}
=============Class===========

Public Class MobileClassDemonstration {
Public Static Void applyDiscount(Set MobileListNew){
for(Mobile__c p : MobileListNew){
if(p.BrandName_c = ‘Samsung’){
p.Price__c = p.Price__c - 20;
}
}
}
}
 
Solution:
 
=============Trigger===========
Trigger MobileDiscountTrigger on Mobile__c (before insert){
if()Trigger.isInsert == True && Trigger.isBefore == True){
MobileClassDemonstration.applyDiscount(MobileClassDemonstration);
}
}
=============Class===========

Public Class MobileClassDemonstration {
Public Static Void applyDiscount(List MobileListNew){
for(Mobile__c p : MobileListNew){
if(p.BrandName__c = ‘Samsung’){
p.Price__c = p.Price__c - 20;
}
}
}
}

 

In the next topic, we will discuss in-detail about “Testing APEX In Salesforce”. 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 TrainingDec 21 to Jan 05View Details
Salesforce TrainingDec 24 to Jan 08View Details
Salesforce TrainingDec 28 to Jan 12View Details
Salesforce TrainingDec 31 to Jan 15View Details
Last updated: 27 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