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.
Table of Contents
Want to enhance your skills in dealing with the world's best CRM, enroll in our Salesforce Online Course.
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.
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.
Solution: The value will be - (Trigger.New = Harry | 20 | NY)
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.
(Trigger.New = Larry | 20 | NY)
(Trigger.Old = Harry | 20 | NY)
Logic - Instead of “Harry”, Employee Name “Larry” is updated.
(Trigger.New = Jerry | 20 | NY)
(Trigger.Old = Larry | 20 | NY)
Solution: The value will be - (Trigger.New = Jerry | 20 | NY)
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.
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
=============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;
}
}
}
}
=============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.
Mindmajix offers different Salesforce certification training according to your desire with hands-on experience on Salesforce concepts
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:
Name | Dates | |
---|---|---|
Salesforce Training | Dec 21 to Jan 05 | View Details |
Salesforce Training | Dec 24 to Jan 08 | View Details |
Salesforce Training | Dec 28 to Jan 12 | View Details |
Salesforce Training | Dec 31 to Jan 15 | View Details |
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.