Blog

Context Variables In Salesforce

  • (4.0)
  • | 24 Ratings |
  • Last Updated February 13, 2019

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

Get ahead in your career by learning Salesforce through Mindmajix Salesforce Training.

Trigger.new - Assume that it is going to give us lists of records due to which trigger got called.

What is 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 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

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

Frequently Asked Salesforce Interview Questions & Answers

Program Logic#3: How to call a code in trigger which client wants to call, when 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---

Program Logic#4: How to call a code in trigger which client wants to call, before the record gets deleted.

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

Subscribe For Free Demo

Free Demo for Corporate & Online Trainings.


DMCA.com Protection Status