Working with Cookie
This is the Cookie class
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/apex_classes_sites_cookie.htm
PageReference methods:-
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/apex_System_PageReference_methods.htm
For example;- I have 2 VF pages.
1. CookieTest1
2. CookieTest2
We will create cookies in CookieTest1 page level and access those cookies in CookieTest2 page level.
Below the code checkout.
1. CookieTest1
<apex:page controller="CookieTest1Controller">
<apex:form >
<apex:commandButton value="Click" action="{!createCookies}"/>
</apex:form>
</apex:page>
Controller:-
public class CookieTest1Controller
{
public CookieTest1Controller()
{
}
public pagereference createCookies()
{
Account objAccount = [SELECT Id from Account limit 1];
Cookie objCookies = new Cookie('accId',objAccount.Id ,null,-1,false);
pagereference pg = new pagereference('/apex/CookieTest2');
pg.setCookies(new Cookie[]{objCookies});
return pg;
}
}
Here we are creating cookie and setCookies.
After that we will get this cookie in second page.
2. CookieTest2
<apex:page controller="CookieTest2Controller">
<apex:detail subject="{!AccId}"/>
</apex:page>
Controller:-
public class CookieTest2Controller
{
public Id AccId {get;set;}
public CookieTest2Controller()
{
Cookie counter = ApexPages.currentPage().getCookies().get('accId');
AccId = Id.valueOf(counter.getValue());
}
}
Based on accId we will get the Account id which was stored in cookie.
No comments:
Post a Comment