Last updated on Nov 21, 2023
Trigger context variables offer details about the Trigger that is currently running. We need a mechanism to find information about the Trigger that is being executed while writing Trigger logic. Trigger context variables come into play here. We may get information about the Trigger as well as the records for which the Trigger is being executed by utilizing the Trigger class in Apex. Trigger context variables essentially give us information about the Trigger that is currently running.
Let's have a look at some real-world instances of Context variables in Triggers.
Wish to make a career in the world of salesforce? Start with HKR'S salesforce online training !
The company has asked you, the developer, to implement some code that will count the number of times a contact record in Salesforce has been updated.
On the Contact object, you'll add a custom field called Count_of_Updates__c. There will be no decimal places in the number field. Then, on the Contact object, you'll construct a trigger to ensure that this count field is always up to date.
Some context variables will be used in this example. We're going to write this as a Before Update trigger to make this code as efficient as possible. We won't need to use another DML statement to update the contact as a result of this. Remember that we'll be keeping the code bulky to adhere to best practices.
The context variables we'll need for this code are listed below.
When using Trigger Context Variables, there are a few things to keep in mind.
If the trigger has an After parameter, and no check is in place to update our contact records only in the before update context, an error will be thrown.
Get ahead in your career by learning Salesforce through hkrtrainings Salesforce Training in Hyderbad !
Let's have a look at the rest of the Trigger context variables and see what they can do.
switch on Trigger.operationType {
when BEFORE_INSERT {{
} //Before Insert logic
} when BEFORE_UPDATE {
//Before update logic
} when BEFORE_DELETE {
//Before delete
} when AFTER_INSERT {
//After Insert logic
} when AFTER_UPDATE {
//After Update logic
} when AFTER_DELETE {
//After Delete logic
} when AFTER_UNDELETE {
//After UnDelete logic
}
}
Click here to get frequently asked Salesforce interview questions & answers
The following list explains the trigger context variables considerations.
Conclusion:
Thus we have learned the salesforce context variables and understood the real-world trigger instances’ requirements, solution, and code. We have also gone through looking into various descriptions of trigger context variables such as oldMap, newMap, isAfter, isUpdate, isBefore, etc., and other considerations.
Related Articles:
A technical lead content writer in HKR Trainings with an expertise in delivering content on the market demanding technologies like Networking, Storage & Virtualization,Cyber Security & SIEM Tools, Server Administration, Operating System & Administration, IAM Tools, Cloud Computing, etc. She does a great job in creating wonderful content for the users and always keeps updated with the latest trends in the market. To know more information connect her on Linkedin, Twitter, and Facebook.
Batch starts on 23rd Mar 2024 |
|
||
Batch starts on 27th Mar 2024 |
|
||
Batch starts on 31st Mar 2024 |
|