Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Salesforce Session variables, set and get variables in Session

I want to be able to read / write some variables to the current session in my Salesforce site pages.

I have a site built using Salesforce Sites, I need to store/retrieve some values across all the pages (consider that I am building something similar to a shopping cart). However I cant find any good example on how to read and write variables to the session (anonymous user).

I am using Visualforce pages with several controllers built in Apex.

Regards

like image 532
Tea Bee Avatar asked Feb 09 '11 11:02

Tea Bee


2 Answers

If you are building something like a shopping cart, or a "wizard" where you need to keep controller variables in context from one page view to another, then the best way to do this in VisualForce is to use the same controller.

When the user submits a form ( through actionFunctions, commandButtons, or commandLinks, etc.), and your controller returns a page Reference, the view state is preserved if the new visual force page uses the same controller.

In this way, you could, for example, have the user enter their name and email address using apex:inputField tags on page one. They navigate to page two, which uses the same controller as page one, and the page could reference the same controller variables. Essentially, the controller is still in scope, and so are all the variables that were updates.

Example:

Page one:

<apex:page controller="myController">
   Please enter your name <apex:inputText value="{!shopper_name}"/>
   <br/>
   <apex:commandButton action="{!pageTwo}" value="Click for page two"/>
</apex:page>

Page two:

<apex:page controller="myController">
   You entered: <apex:outputText value="{!shopper_name}" />.
</apex:page>

Controller:

public class myController {
  public string shopper_name { get; set; }
  public myController() {
    shopper_name = null;
  }
}
like image 194
Eric Sexton Avatar answered Sep 27 '22 20:09

Eric Sexton


Custom settings are cached at the application level, maybe that's why it was suggested in the link above. I'm not sure if I'd recommend that approach, but you might be able to get it to work.

If you create a Custom Setting named "SessionData", and add your custom fields (that represent the data you want to store in session), you could save data to it like this:

Database.SaveResult result = Database.insert(new SessionData__c(YourFieldHere='Your value here etc'));
System.debug(result.getID());

Then use the resulting custom setting ID to store in a cookie. While custom settings can be accessed using normal SOQL, the advantage is that the data is cached and can be accessed like this:

if (SessionData__c.getAll().containsKey('unique ID from cookie here'))
{
     System.debug(SessionData__c.getInstance('unique ID from cookie here').YourFieldHere);
}

Keep in mind that custom settings weren't really designed for this, so you'll need to periodically purge old custom settings data, as normal session management systems do.

See the Apex Custom Settings documentation for more details.

like image 24
Adam Butler Avatar answered Sep 27 '22 20:09

Adam Butler