The documentation for the Cookie class is here: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_sites_cookie.htm

But lacks documentation for the constructor. From code examples it looks like the constructor takes 5 parameters:

  1. String name // cookie name
  2. mixed value // cookie content
  3. ?? // normally 'null'
  4. int time // -1 for expiry with session, otherwise number of seconds to live
  5. boolean ?? // normally 'false'

Can someone confirm this and fill in the blanks for us?

Also, how do you know this?

4 Answers 4


I have used cookies in the past, and struggled in the same way that you did. My understanding of the constructor is as follows:

Cookie cook=new Cookie('Name', 'Keir Bowden', null, 1440, false);


  • 'Name' = the name of the cookie
  • 'Keir Bowden' = the value of the cookie
  • null = the cookie path (where null results in the default location '/')
  • 1440 = the max age
  • false = is the cookie only accessible through https

Pretty sure I found this through trial and error and inspecting the HTTP messages - its still my opinion though!

I also recall that the max age didn't seem to have any effect, and I could only generate session cookies, but in the end I couldn't do what I wanted to anyway so I didn't investigate further.

  • If the fifth parameter is to make it HTTPS only I would want that to be true for security reasons (remember Firesheep?)
    – Mike Chale
    Nov 29, 2012 at 11:07
  • Remember cookies are used in Force.com sites, which are often exposed via HTTP. Nov 29, 2012 at 11:49
langCookie = new Cookie('uLang', userLanguage, null, -1, false);
    ApexPages.currentPage().setCookies(new Cookie[]{langCookie});
    userLanguage = langCookie.getValue();

Just agreeing with Bob and this is the code that i used for one of the website development on force.com and works perfectly for me .


I had the same issue, found this thread then went back and told SFDC that they needed to improve their documentation. Then I read the code comments in the sample, d'OH! (emphasis added)

// create a new cookie with name 'counter', an initial value of '1', 
// path 'null', maxAge '-1', and isSecure 'false'. 
    if (counter == null) {
        counter = new Cookie('counter','1',null,-1,false);
  • Still worth it to open a docs bug by hitting the "Yes, except" at the bottom of the page. That automatically opens a case for the docs folks. Apr 27, 2013 at 0:39

May be this helps ..

Below is code sample to read and set apex cookies.

//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[] {

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.