What are Context Variables in Salesfoce Tiggers ? | A Definitive Guide [ OverView ]
Last updated on 23rd Dec 2021, Blog, General
All triggers define implicit variables that allow developers to access run-time context. These variables are contained in the System. Trigger class.
- Introduction Trigger context variables in salesforce
- List of Trigger Context Variables
- Trigger Context Variables Considerations
- Actions Performed By Trigger Events
- Why Is Running Triggers Important?
- What is Trigger in Salesforce?
- Types of triggers
Introduction Trigger context variables in salesforce
All triggers outline implicit variables that permit builders to get entry to run-time context. These variables are contained withinside the System.Trigger class.
- Trigger.isExecuting: Returns authentic if the cutting-edge context for the Apex code is a cause, now no longer a Visualforce page, a Web service, or an executeanonymous() API call.
- Trigger.isInsert: Returns authentic if this cause turned into fired because of an insert operation, from the Salesforce person interface, Apex, or the API.
- Trigger.isUpdate: Returns authentic if this cause turned into fired because of an replace operation, from the Salesforce person interface, Apex, or the API.
- Trigger.isDelete: Returns genuine if this cause became fired because of a delete operation, from the Salesforce consumer interface, Apex, or the API.
- Trigger.isBefore: Returns genuine if this cause became fired earlier than any file became saved.
- Trigger.isAfter: Returns genuine if this cause became fired in the end statistics have been saved.
- Trigger.isUndelete: Returns genuine if this cause became fired after a file is recovered from the Recycle Bin (that is, after an undelete operation from the Salesforce consumer interface, Apex, or the API.)
- Trigger.new: Returns a listing of the brand new variations of the sObject statistics. This sObject listing is best to be had in insert, replace, and undelete triggers, and the statistics can best be changed in earlier than triggers.
- Trigger.newMap: A map of IDs to the brand new variations of the sObject statistics. This map is best to be had in earlier than replace, after insert, after replace, and after undelete triggers.
- Trigger.vintage : Returns a listing of the vintage variations of the sObject statistics. This sObject listing is best to be had in replace and delete triggers.
- Trigger.oldMap: A map of IDs to the vintage variations of the sObject statistics. This map is best to be had in replace and delete triggers.
- Trigger.size: The overall variety of statistics in a cause invocation, each vintage and new.
List of Trigger Context Variables
Following are the context variable to be had in triggers. Please notice variable availability in cause varies in step with the sort of cause events.Here is List of Trigger Context Variables are:-
List of all Trigger Context Variables
Trigger Context Variables Considerations
1. Trigger.new and trigger.antique can’t be utilized in Apex DML operations.
2. You can use an item to alternate its personal area values the usage of trigger.new, however most effective in earlier than triggers. In all after triggers, trigger.new isn’t always saved, so a runtime exception is thrown.
3. trigger.antique is constantly read-most effective.
4. You can’t delete trigger.new.
- beforeinsert – This occasion plays the interest of converting fields thru the platform of cause.new.
- afterinsert – This occasion can bask in updating an authentic Salesforce item with the assist of an “replace DML” operation.
- beforeupdate – This occasion is most effective restrained to converting fields the use of cause.new.
- afterupdate – This occasion is able to updating an authentic item thru an “replace DML” operation. It also can delete an authentic item with the assist of a “delete DML” operation.
- beforedelete – This occasion can most effective replace an authentic item with the assist of an “replace DML” operation.
- afterdelete – This cause isn’t always capable of carry out majority of the movements as cause.new isn’t always to be had for the after delete triggers. Also, it can not be used for updating items because the item is already deleted.
- afterundelete – This occasion can not extrade fields with the assist of cause.new as doing the equal might display a runtime blunders and cause.new can not be availed with the aid of using undelete triggers. However, it is able to be used for updating an authentic item the use of an “replace DML” operation. It also can be used for deleting the authentic item with the assist of “delete DML” operation however it might be pointless to do so, because of the item being right away deleted as it’s miles inserted into the system.
Actions Performed By Trigger Events
As stated above, builders use distinct cause occasions in Salesforce to adopt unique operations bearing on statistics manipulation language. Here are a number of the important thing movements done with the aid of using the ones cause occasions:-
Develop Your Skills with Salesforce Developer Certification TrainingWeekday / Weekend BatchesSee Batch Details
Why Is Running Triggers Important?
It is vital for Salesforce Developers to run positive particular triggers as they assist the customers in enhancing different statistics belonging to the identical kind as that of the statistics that initiated the trigger. As triggers can motive this transformation of statistics and also can cause firing of greater triggers, the runtime engine of Apex units positive limits at the range of operations a person can carry out for stopping limitless recursion.
- Inserting statistics into the system.
- Updating statistics in the system.
- Deleting statistics from the system.
- Merging statistics throughout platforms.
- Upserting statistics into the system.
- Undeleting statistics from the system.
- A desirable instance of builders the usage of triggers in Salesforce may be that of strolling a cause earlier than or after statistics are restored into Recycle Bin. Triggers may be described for items belonging to the top-stage standards. These are the items that assist triggers, which includes Contacts or Accounts, referring to a database. Triggers also can be described for positive wellknown infant items like CaseComment and different applicable custom items.
- You can outline triggers in Salesforce through deciding on the choice of “Triggers” for the item whose triggers you need to get admission to from the choice of “Object Management Settings”.
What is Trigger in Salesforce?
As cited above, triggers are the additives used to awaken Apex in Salesforce that lets in builders to adopt precise and customized actions, earlier than or after modifications are made in Salesforce statistics. These modifications can be concerning updating, deleting or placing essential statistics into the system.
Trigger in Salesforce is largely an Apex script utilized by builders earlier than or after occasions associated with statistics manipulation language (DML). Here are a number of the important thing styles of operations finished through a cause:
- Before Triggers – These are the triggers run for updating or validating essential document values earlier than they may be stored to the platform database.
- After Triggers – These are the triggers which can be read-handiest in nature and permit builders to get right of entry to the sphere values set through the machine and have an impact on adjustments going on in special records (together with firing asynchronous activities the use of a queue or logging into an audit table).
Types of triggers
There are widely styles of triggers that may be run through builders:-
Thus we’ve found out the salesforce context variables and understood the real-global cause instances’ requirements, solution, and code. We have additionally long gone via searching into numerous descriptions of cause context variables consisting of oldMap, newMap, isAfter, isUpdate, isBefore, etc., and different considerations.