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.
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.
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. |
Trigger in Apex Class:
---Code---
---Code---
if(Trigger.isExecuting == True){
---Code---
}
---Code---
---Code---
---Code---
isInsert
”: Skeletal Structure of program with Logic---Code---
---Code---
if(Trigger.isInsert == True){
---Code---
}
---Code---
---Code---
---Code---
---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
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.
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 | Nov 23 to Dec 08 | View Details |
Salesforce Training | Nov 26 to Dec 11 | View Details |
Salesforce Training | Nov 30 to Dec 15 | View Details |
Salesforce Training | Dec 03 to Dec 18 | 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.