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
GoForceGoGoForceGo 

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?
 


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;
}

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;
          }
    }
 }
}

 



Ron HessRon Hess
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?

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.


GoForceGoGoForceGo
Thanks.

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.




GoForceGoGoForceGo
What would happen if I have two different Web Services call and set p.FirstRun false in the first call.
Do the two calls translate into two different instances of class p? In that case, the following code would
would be meaningless and inaccurate.



Code:
WebService static void setfirstRun (boolean val) {
   p.FirstRun = val;
}

WebService static void doSomething() {

  if (p.FirstRun) {
      doFirstRunStuff;
  } else {
     doOtherthings;
  }
}


//call these webservices for s-control or a client app.

setFirstRun(true);
doSomething();



 

SuperfellSuperfell
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.

http://www.salesforce.com/us/developer/docs/apexcode/index_CSH.htm#apex_classes_static.htm#using_static_methods_and_variables
GoForceGoGoForceGo
What is the definition of a "request"?

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 ?


SuperfellSuperfell
request == a single HTTP Request.
champ_vimalchamp_vimal

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

Volker_factory42Volker_factory42

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.

Note that static variable values are reset between batches, but governor limits are not. Do not use static variables to track state information between batches.

 But I tried it, and the value of my static variable was not reset.

 

in Trigger:
...
static boolean hasAlreadyDone = false;
if(!hasAlreadyDone){
 Trigger.new[0].addError('First Record error');
 hasAlreadydone = true;
}
...

 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...

craigmhcraigmh

You're thinking of global variables.