Access variables in @Remoteaction
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.
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.