开发者

Salesforce Session variables, set and get variables in Session

开发者 https://www.devze.com 2023-02-09 17:46 出处:网络
I开发者_如何学C want to be able to read / write some variables to the current session in my Salesforce site pages.

I开发者_如何学C 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


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;
  }
}


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.


I think Visualforce View State might be useful to you:

Visualforce pages that contain a form component also contain an encrypted, hidden form field that encapsulates the view state of the page. This view state is automatically created, and as its name suggests, it holds the state of the page - state that includes the components, field values and controller state.


You should use Javascript cookies for this.

You could also use Apex cookies, but then you'd need to make sure that each request hits the server (and not the caching layer).

for Apex Cookie you can use following code:

//Setting Cookie
public void setCookie() {
    Cookie userCookie = new Cookie('CookieName', fieldValueToBeStoredAsCookie, null, 315569260, false); //Here 315569260 represents cookie expiry date = 10 years. You can set this to what ever expiry date you want. Read apex docs for more details.
    ApexPages.currentPage().setCookies(new Cookie[] {
        userCookie
    });
}

//Reading Cookie 
Cookie cookie = ApexPages.currentPage().getCookies().get('CookieName');
if (cookie != null) {
    String fieldValueToBeStoredAsCookie = cookie.getValue();        
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号