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

Storing and Accessing of Session variables in APex Page aswell in controller

How to store variables in Session and how to access that Session Variable in Apex Page & in Contoller?.




This is a bit of tricky topic as it depends on what you'd like to do and what constitutes a session for you. Unfortunately there aren't class to set session variables like you might find with JSP or PHP, but you can try a few of these options,


1. You can create a session if all pages involved use precisely the same controller and controller extensions. A tutorial can be found here


2. You could use cookies.


3. You could use Custom Settings, a new feature on the Platform. 




Hi Wes, 
Thanks for your reply. I am looking the following equivalent like storing a value in Session Object in Apex Class and Retreving it in Diff  VFPage.
 Storing a value in Session Object :

String sessionSet="theKK"; session.setAttribute("MySession",sessionSet);


 Retreiving the value from Session Object  :

String getSessionValue= (String)session.getAttribute("MySession");


Thanks & Regards ,



Yeah I've had similar requirement before but there isn't a feature available as you've described it. I would recommended reading throught the options I've summarised, and choosing one from there. If the pages are completely different and it doesn't make sense for them to use the same controller I'd suggest using 'Custom Settings'.




In first Page I've selected a Organization, we need to get that selected organization in all pages similar to Session Tracking.

Susana NTSSusana NTS

Hi all,


I have the same requirement as Krish.


The thing is that I can't see the point in using Custom Settings since, as it says in the documentation, "After you save a custom setting, you cannot change this value".

How do you plan to use that feature if you are unable to set an specific value each time a user enters the page?





Susana NTSSusana NTS

I think this reply to another post is one of the solutions to our requiremet




I have the same question, and the provided answers do not seems to be problem solvers.


  • Using cookies is not possible, I want more secure place to store the values
  • Using the same controller also is not possible, and I doubt that this solution will work in case the user hit CTRL+F5
  • I cant find any example on how to read/write to custom settings (as mentioned in one of the answers) in Apex for each anonymous user, I want each user's data to be accessible only by him