What is the context variable and explain with example ?

782    Asked by AmeliaArnold in Salesforce , Asked on Jan 2, 2020
Answered by Amelia Arnold

Basically, all the triggers describe implicit variables which allow the salesforce developers to control run time context. All these variables are stored in the system.trigger class.

So In simple terms, salesforce developers can control the execution of particular code in triggers based on these context variables.

Please find a list of context variables available in apex trigger :-

Variable

Usage

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. This recovery can occur after an undelete operation from the Salesforce user interface, Apex, or the API.

new

Returns a list of the new versions of the sObject records.

This sObject list is only available in insert, update, and undelete triggers, and the records can only be modified in before triggers.


newMap

A map of IDs to the new versions of the sObject records.

This map is only available in before update, after insert, after update, and after undelete triggers.


old

Returns a list of the old versions of the sObject records.

This sObject list is only available in update and delete triggers.


oldMap

A map of IDs to the old versions of the sObject records.

This map is only available in update and delete triggers.


operationType

Returns an enum of type System.TriggerOperation corresponding to the current operation.

Possible values of the System.TriggerOperation enum are: BEFORE_INSERT, BEFORE_UPDATE, BEFORE_DELETE,AFTER_INSERT, AFTER_UPDATE, AFTER_DELETE, and AFTER_UNDELETE. If you vary your programming logic based on different trigger types, consider using the switch statement with different permutations of unique trigger execution enum states.


size

The total number of records in a trigger invocation, both old and new.

Example of Context variable use in apex trigger :-

trigger testAccountTrigger on Account(before delete, before insert, before update) {

if (Trigger.isBefore) {

    if (Trigger.isDelete) {

        // In a before delete trigger, the trigger accesses the records that will be

        // deleted with the Trigger.old list.

        for (Account a : Trigger.old) {

            if (a.name != 'InactiveAccount') {

                a.addError('We can’t delete this Account!');

            }

        }

    } else {

    // In before insert or before update triggers, the trigger accesses the new records

    // with the Trigger.new list.

        for (Account a : Trigger.new) {

            if (a.name == 'Good') {

                a.name.addError('Good name');

            }

    }

  }

     }

         }



Your Answer

Interviews

Parent Categories