2

I am trying to pass an argument from a webService function within a global class and assign it to global variable within that class, but when I try to call the value within the class, or even within that function, it states I haven't defined the variable.

What am I not understanding about scope?

global class JSONToApexWrapper{

global boolean awsUpdate;

    webservice static void getInformation(String customerId, boolean awsCallRequest) {

         awsUpdate = awsCallRequest;

    }
}

Variable does not exist: awsUpdate

2 Answers 2

3

Non-static variables (commonly called instance variables) are specifically contained in an instance of a class. Static variables, contrariwise, are stored in class' static storage area, of which only one exists per transaction. You cannot access an instance variable in a static method, because there is no instance to act upon. It's the same reason why you can't use "this" in a static method, because there is no "this" to act on, since you're in the class' static storage area. Alternatively, you might say that static methods have no "scope", because they are outside of all instances of the object.

+-----------------[ Class Layout ]---------------------------------+
| +----------+       +------------+                                |
| |          |       | Instance 1 | "this" refers to Instance 1    |
| |  Static  |       +------------+                                |
| |  Storage |       | Instance 2 | "this" refers to Instance 2    |
| |   Area   |       +------------+                                |
| |          |       |     ...    | "this" refers to instance N    |
| +----------+       +------------+                                |
|  No "this"                                                       |
|  variable                                                        |
+------------------------------------------------------------------+

Where each box represents a single copy of data. The static area is initialized only once, and can be accessed by all static and instance code. Instance data, however, must explicitly refer to a single instance of storage area. Since the static storage area is outside of all the instances, you can't access instance variables in static methods.

1
  • This helped me understand this tremendously. Thank you!
    – S.B.
    May 10, 2018 at 20:34
2

You can't assign instance variables from a static context.

Instead of:

global boolean awsUpdate;

Use:

global static Boolean awsUpdate;

Or consider public/private access modifiers if they are more appropriate.

1
  • So because webservice is static, I cannot assign it to the global instance boolean variable? I would think awsUpdate would still exist and be undefined... Any tips on how you understand static / instance other than the docs: developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
    – S.B.
    May 9, 2018 at 21:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .