0

I have a situation where I need to keep some data in a class structure throughout my application and this data differs for every user.

What I tried is :

I created a class structure and instantiated in one JS remoting call and tried accessing it in another remoting call but I was unsuccessful in fetching the data.

My Code :

public class MyInstance{
     public static string myData;
}

@RemoteAction
public static string setuserData(){
     MyInstance.myData = 'MyName';
}

@RemoteAction
public static string getuserData(){
     return MyInstance.myData;
}

When I call getUserData after setUserData I get null.

Please help me out, how I can achieve what I want here.

1 Answer 1

1

When using @RemoteAction you have to either pass all the information from the client-side (or save the information into the database in one request and pull it out in subsequent requests).

Some background explanation is that requests from Visualforce pages are processed by a group of server machines to provide scalability and reliability. So any server-side state (except that in the database) is discarded after a page has been generated as there is no guarantee that the same server will be used for the next request. This leads to the idea of view state that is passed between the server and the client: this allows non-static and non-transient properties to be populated in the controller automatically.

But this view state process is quite time consuming, so it is useful to have an option where it is not applied: @RemoteAction calls. If you want it to be applied use apex:actionFunction instead (and make sure your property is not static). See Comparing JavaScript Remoting and <apex:actionFunction> for some more information.

You must log in to answer this question.

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