function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jagdish nikamjagdish nikam 

Static Keyword in trigger

Why we don't use static keyword in trigger? i am new to the salesforce. i know little bit about statc keyword but want answer in this case.
Best Answer chosen by jagdish nikam
Prateek Singh SengarPrateek Singh Sengar
Hi Jagdish,
You can use static variables in trigger however they are not usually recommended. Some of the main reasons behind this is
  • It is bad practice to write logic inside your trigger, minimizing the amount of code in a trigger and instead implement all functionality in class.
  • Having static variables separated in a class will help you with the unit test by loading up the static variables with the required data and not rely on the fact that you have to trigger the trigger in order to populate with test data test your scenario.
  • Also the most important thing to know about triggers is that when you have more than one trigger of the same type, you cannot predict the order in which they will fire. So if you have a static variable in a trigger that has a value that another trigger will depend on than you would have some problems.
If my post was of any help please like it, if it answers your question please mark it as solution.

All Answers

Prateek Singh SengarPrateek Singh Sengar
Hi Jagdish,
You can use static variables in trigger however they are not usually recommended. Some of the main reasons behind this is
  • It is bad practice to write logic inside your trigger, minimizing the amount of code in a trigger and instead implement all functionality in class.
  • Having static variables separated in a class will help you with the unit test by loading up the static variables with the required data and not rely on the fact that you have to trigger the trigger in order to populate with test data test your scenario.
  • Also the most important thing to know about triggers is that when you have more than one trigger of the same type, you cannot predict the order in which they will fire. So if you have a static variable in a trigger that has a value that another trigger will depend on than you would have some problems.
If my post was of any help please like it, if it answers your question please mark it as solution.
This was selected as the best answer
jagdish nikamjagdish nikam
Thank you for this detail information....!!!!