function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
admintrmpadmintrmp 

Cookies gone wrong!

I am trying to get a cookie value.

 

If I use getValue(), my Sites application will return an authorization error.

If I use String.valueOf(cookie), it returns the value as null.

 

What am I doing wrong here?

 

Here is my cool new cookie class:

 

public class coreCookie {
// ----------------------------------------------------
// Check if cookie exists
// ----------------------------------------------------
public Boolean exists(String cookieName) { return get(cookieName) != null; }
// ----------------------------------------------------
// Get the cookie value (if exists)
// ----------------------------------------------------
public Cookie get(String cookieName) { return ApexPages.currentPage().getCookies().get(cookieName); }
public String getStr(String cookieName) { return String.valueOf(ApexPages.currentPage().getCookies().get(cookieName)); }
// ----------------------------------------------------
// Set the cookie value
// ----------------------------------------------------
public Cookie set(String cookieName, Integer cookieVal) { return this.set(cookieName, String.valueOf(cookieVal)); }
public Cookie set(String cookieName, String cookieVal) {
Cookie cval = new Cookie(cookieName, cookieVal, null, -1, false);
return get(cookieName);
}
}

 

Best Answer chosen by Admin (Salesforce Developers) 
AddisonAddison

Hi admintrmp,

 

It seems like in your set method, the cookie is not actually being added to the page.  Try adding this line in your 2nd "set" method after the "Cookie cval = new ...."

 

 

ApexPages.currentPage().setCookies(new Cookie[]{cval});

 

 

The Cookie(stuff...) constructor only creates a new cookie instance, and that instance needs to be actually set for the page.  Hope this helps.

 

Addison

All Answers

AddisonAddison

Hi admintrmp,

 

It seems like in your set method, the cookie is not actually being added to the page.  Try adding this line in your 2nd "set" method after the "Cookie cval = new ...."

 

 

ApexPages.currentPage().setCookies(new Cookie[]{cval});

 

 

The Cookie(stuff...) constructor only creates a new cookie instance, and that instance needs to be actually set for the page.  Hope this helps.

 

Addison

This was selected as the best answer
AddisonAddison

Also, if your Sites app is returning an authorization error, make sure your apex page and the apex class are allowed for the Site guest user's profile, accessible through: Site Details -> Public Access Settings.  Or if it's for a Site portal, go to the profile of the portal that's assigned to the site.

admintrmpadmintrmp

That's it!

 

I thought that instantiating the Cookie object will automatically set the cookie. setCookies() is the appropriate solution!

 

Thankyou!

AddisonAddison

You're welcome!

 

Being able to use setCookies() on a page gives you the freedom to set different cookies on different Apex pages.