I'm refactoring an embedded Visualforce page for a Lightning upgrade project. Previously an error message was passed via a URL parameter, but that no longer works due to an additional redirect (apparently as Lightning handles Classic URLs--not by us), so I'm setting the value on a cookie. I can see that the cookie is being set as expected on the embedded frame with the prefix apex__ and the URL-encoded message as a value.

setCookie('errorMsg', errorMessage); // cookie name ends up as 'apex__errorMsg'

private void setCookie(String name, String msg) {
    if (msg != null) {
        Cookie errorMsgCookie = new Cookie(name, msg, null, -1, false);
        ApexPages.currentPage().setCookies(new Cookie[] {errorMsgCookie});

I'm trying to retrieve that value in the class constructor and it comes back null.

this.errorMsgCookieVal = getCookieVal('errorMsg');

private String getCookieVal(String name) {
    Cookie cookie = ApexPages.currentPage().getCookies().get(name);
    System.debug('updating cookie named: ' + name);

    return cookie == null ? null : cookie.getValue();

Do I have an issue with mismatched page refs? Does that matter? I noticed that the HttpOnly, Secure, and SameSite values aren't populated on that cookie, whereas the other cookies have those values. Is that an issue?

It's also maybe worth mentioning that the parent page and embedded pages are on different subdomains, respectively:




It turns out that the answer isn't really a Salesforce thing. By setting the max-age value to -1 (per an example I had encountered), the cookie was immediately expired. By changing it to a valid positive value the expected value was returned.

It's unclear to me why an expired cookie shows in Chrome's Application panel, or whether JavaScript would have retrieved the value where Apex doesn't, but there it is.

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.