[SalesForce] Access variables in @Remoteaction

I have a @RemoteAction function in my Apex Class. I have a public static variable in this class. When I access this variable in the @RemoteMethod, there is error saying variable doesn't exist. When I make the class, method, and variable all global, the code compiles, but the variable is still null.

Can anyone help me in what am I doing wrong?

Best Answer

Notice that your RemoteAction is static. If the static variable you are trying to reference is set by instance logic in your controller, the new transaction context of the RemoteAction will lose that stateful information.

Works:

static Object myProperty
{
    get
    {
        if (myProperty == null)
        {
            // instantiate
        }
        return myProperty;
    }
    private set;
}

Doesn't Work:

static Object myProperty;
public MyController()
{
    myProperty = somethingStateful;
}

If myProperty depends on stateful information, it's not really static after all, even if you declare it so. Any state you want to maintain in your RemoteAction must be passed through parameters.

Related Topic