You need to sign in to do that
Don't have an account?

Static Variables - are they shared by all Salesforce threads within an org?
I am wondering how do static variables behave in Salesforce. Is the variable shared by all the threads/users within an org?
In the following example, p.FirstRun is set to false inside the trigger.
What happens when :
1. At the same time user 1 is executing this trigger, user 2 is also executing the same trigger? Do they overwrite each other's value?
2. What happens when 10 minutes later someone else needs to execute the same trigger. Is the p.FirstRun set to false by the the first
execution of the trigger? Does the trigger below need to reset p.FirstRun to true when it comes back in the second time for recursion so that a second user
ten minutes later find p.Firstrun as true?
In the following example, p.FirstRun is set to false inside the trigger.
What happens when :
1. At the same time user 1 is executing this trigger, user 2 is also executing the same trigger? Do they overwrite each other's value?
2. What happens when 10 minutes later someone else needs to execute the same trigger. Is the p.FirstRun set to false by the the first
execution of the trigger? Does the trigger below need to reset p.FirstRun to true when it comes back in the second time for recursion so that a second user
ten minutes later find p.Firstrun as true?
Code:
From Salesforce Apex Language reference manual:
Use static variables to store information that is shared within the confines of the class. All instances of the same class share a
single copy of the static variables. For example, all triggers that are spawned by the same request can communicate with each
other by viewing and updating static variables in a related class. A recursive trigger might use the value of a class variable to
determine when to exit the recursion.
A trigger that uses this class could then selectively fail the first run of the trigger:
Suppose you had the following class:
public class p {
public static boolean firstRun = true;
}
From Salesforce Apex Language reference manual:
Use static variables to store information that is shared within the confines of the class. All instances of the same class share a
single copy of the static variables. For example, all triggers that are spawned by the same request can communicate with each
other by viewing and updating static variables in a related class. A recursive trigger might use the value of a class variable to
determine when to exit the recursion.
A trigger that uses this class could then selectively fail the first run of the trigger:
Suppose you had the following class:
public class p {
public static boolean firstRun = true;
}
trigger t1 on Account (before delete, after delete, after undelete) { if(Trigger.isBefore){ if(Trigger.isDelete){ if(p.firstRun){ Trigger.old[0].addError('Before Account Delete Error'); p.firstRun=false; } } } }
Since the two different users are causing two different trigger requests, these will have different instances of the class 'p', so the answer is no.
2. What happens when 10 minutes later someone else needs to execute the same trigger. Is the p.FirstRun set to false by the the first execution of the trigger? Does the trigger below need to reset p.FirstRun to true when it comes back in the second time for recursion so that a second user ten minutes later find p.Firstrun as true?
10 min later is a seperate request, so seperate class, so new instance of the class 'p', no need to reset the firstRun
i've not tested this, but this is my understanding from reading the developer guide.
I thought the whole point of a static variable in a class was to have a single copy of the that variable that is shared by the entire application.
A non-static variable is the one that gets instantiated when a user does "new" on the class and instantiates the class.
Seems like what you are saying is that each user request creates a new copy of the class itself.
Do the two calls translate into two different instances of class p? In that case, the following code would
would be meaningless and inaccurate.
http://www.salesforce.com/us/developer/docs/apexcode/index_CSH.htm#apex_classes_static.htm#using_static_methods_and_variables
Is each apex call from an Scontrol a separate request ? If I have two Apex calls from same S-control, do they get two different instances of class' static variables or a single instance ?
Is each Webservice call from an external application a separate request ?
Is there a notion of a "session object" (httpSession) where user retains static variable through out the session?
If session object exists, are static variables per request or per session ?
Hi Simon,
As per your statement: "its not totally clear form the docs but the scope of static does not extend outside the boundary of a request, they are per request static, not per user, per organization, per server or per application. The only thing that extends outside the scope of the request is whatever you write to the database." , I understood that the scope of static is per http request.
Now, the doc also says that: "all triggers that are spawned by the same request can communicate with each other by viewing and updating static variables in a related class. A recursive trigger might use the value of a class variable to determine when to exit the recursion." I am not getting the meaning of this statement. Triggers calling same class in their individual requests may change values of static variables, so per request too, the static variable value changes as per what I understand! Can you please clarify me and simplify what the doc says?
Thanks,
Vimal
I've a question regarding bulk:
In the mentioned example it is easy with <200 reocords.
BUT:
If I want to insert i.e. 410 records, and I have a static variable which avoid running trigger twice (i.e. insert, update), it should be reset after first batch of 200.
But I tried it, and the value of my static variable was not reset.
My goal is to update more than 200 records, but if the triger fires again in this transaction, he should not do the code. Only when the next batch with next 200 records will be updated...
You're thinking of global variables.