function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RIyas BasheerRIyas Basheer 

How to preserve variable state between two Visualforce Javascript Remoting requests

Hi All,

Is there any way to preserve variable state between  two successive Visualforce Javascript Remoting requests?

For eg:-
class myOpportunities{
      public static list<opportunity> currentOpportunities;
   
       @RemoteAction
       public static string getOpportunities(){
           myOpportunities.currentOpportunities = [SELECT id, Name, status FROM opportunity WHERE CreatedDate = LAST MONTH];
         
       JSONGenerator gen = JSON.createGenerator(true);
       	 gen.writeStartObject();
       	 gen.writeObjectField('data', myOppertunities.currentOppertunities);
       	 gen.writeEndObject();
       	 jsonData = gen.getAsString();	
       	 
       	 return jsonData;

       }

       @RemoteAction
       public static integer getClosedOpportunityCount(){
             integer count = 0;
             for(opportunity opp: myOpportunities.currentOpportunities){
                            if(opp.ststus == 'Closed'){
                                 count++;
                            }

             }

         return count;
       }

}
getOpportunities functiong(); called on page load  & getClosedOpportunityCount(); called on a button click.

on debugging I am getting null value for myOpportunities.currentOpportunities on getClosedOpportunityCount() function.

Is there any way to do this?
Best Answer chosen by RIyas Basheer
bob_buzzardbob_buzzard
You can't do this - JavaScript remoting is stateless so there is nothing available based on previous requests, instances of the controller etc.  If you need to retain anything from the request you have to send it back to the page and then include it in the next request. Statics don't help in this instance as Apex statics get discarded at the end of the request (transaction), rather than being retained through the lifetime of the virtual machine as they would in Java.

If you want to set up values in the controller that are available across requests, you'll have to use regular Visualforce and maintain the state of the controller in the view state, with all its attendent effects on performance etc. 

All Answers

logontokartiklogontokartik
I dont think you can access the variables that way in Remote Action Method, by definition they are stateless and a new copy is created everytime a method is invoked. If you want to access something, its better off passing it as an argument to the method itself. 

But from the way the logic is built, I feel you dont really need a 2nd remote action method to get the  closed count, as you already have the opportunities available to you in your VF Page, can you not build the count in the Javascript itself?


RIyas BasheerRIyas Basheer
thanks logontokartik,

Actually this is sample scenario, I try to explain it simple as possible.
bob_buzzardbob_buzzard
You can't do this - JavaScript remoting is stateless so there is nothing available based on previous requests, instances of the controller etc.  If you need to retain anything from the request you have to send it back to the page and then include it in the next request. Statics don't help in this instance as Apex statics get discarded at the end of the request (transaction), rather than being retained through the lifetime of the virtual machine as they would in Java.

If you want to set up values in the controller that are available across requests, you'll have to use regular Visualforce and maintain the state of the controller in the view state, with all its attendent effects on performance etc. 

This was selected as the best answer