6

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?

2 Answers 2

6

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.

7

Remote action runs in another context so you don't have access to controller variables (think of it like a @future method). You can pass some Ids or something similar to your remote action and then query it from your Database if you need. Also serialization might work if you want to pass an object.

You must log in to answer this question.

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