Salesforce Session Variables, Set and Receive Variables in a Session

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

I have a site created using Salesforce sites, I need to store / retrieve some values ​​on all pages (think I'm creating something like a shopping cart). However, I cannot find a good example of how to read and write variables to a session (anonymous user).

I use Visualforce pages with several controllers built into Apex.

Hello

+4
source share
4 answers

User settings are cached at the application level, which is probably why it was suggested in the link above. I'm not sure if I recommend this approach, but you can make it work.

If you create a custom setting called "SessionData" and add your custom fields (which represent the data that you want to save in the session), you can save the data in it as follows:

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

Then use the resulting custom ID parameter to store in the cookie. While user preferences can be accessed using regular SOQL, the advantage is that the data is cached and can be accessed as follows:

 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 user settings have not been developed for this, so you need to periodically clear the old user settings data, as conventional session management systems do.

For more information, see Apex User Preferences Documentation .

+3
source

If you create 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 a user submits a form (via actionFunctions, commandButtons or commandLinks, etc.) and your controller returns a link to the page, the view state is saved if the new page of visual power uses the same controller.

Thus, you can, for example, specify the username and email address using apex: inputField tags on the first page. They go to page two, which uses the same controller as the first page, and the page can refer to the same controller variables. Essentially, the controller is still in scope, and all the variables that were updates are.

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; } } 
+3
source

I think Visualforce Visibility State may be useful to you:

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

0
source

To do this, you should use the Javascript cookie .

You can also use Apex cookies , but then you will need to make sure that each request goes to the server (and not to the cache level).

for Apex Cookie you can use the 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
source

Source: https://habr.com/ru/post/1339012/


All Articles