You need to sign in to do that
Don't have an account?
admintrmp
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);
}
}
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 ...."
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
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 ...."
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
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.
That's it!
I thought that instantiating the Cookie object will automatically set the cookie. setCookies() is the appropriate solution!
Thankyou!
You're welcome!
Being able to use setCookies() on a page gives you the freedom to set different cookies on different Apex pages.