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
MTBRiderMTBRider 

Setting cookies for pages that are not the current page

Does anyone know how to set a cookie for a page that is not the current page?  I have page1 calling page2.  When page2 is .currentPage(), I want to set a cookie for page1.

 

I tried doing this:

PageReference page1 = new PageReference('/apex.page1');
Cookie cookieMsg = new Cookie('msg', 'My message', null, -1, false);      
page1.setCookies(new Cookie[]{cookieMsg});

 But that did not work.  

 

Thanks for your help.

Best Answer chosen by Admin (Salesforce Developers) 
MTBRiderMTBRider

Ok, thanks.  Based on your information, I went back, took a closer look, and figured out what the problem was.  Turns out, the cookie that page2 was trying to set was never getting set because the page2 window was being closed before it had a chance to set the cookie.

 

---There is a command button on page2 that has an action to set the cookie in the controller method ok() and then close the window:

<apex:page>
   ...
   <apex:commandButton action="{!Ok}" value="Assign" onClick="CloseWindow()"/>

   <script>
      function CloseWindow() {
         var winMain=window.opener;
         if (null==winMain) {
            winMain=window.parent.opener;
         }
         winMain.closePopup();
       }
   </script>
</apex:page>


--Changing the CloseWindow() function to fire on onComplete instead of onClick allowed time for ok() to actually set the cookie...

 ...
   <apex:commandButton action="{!Ok}" value="Assign" onComplete="CloseWindow()"/>
...

 Thanks for your help.

All Answers

sfdcfoxsfdcfox

What are you trying to accomplish? I don't think you're asking the right questions...

MTBRiderMTBRider

I am trying to set a message that page1 can retrieve while in page2.  I am trying to do this via a cookie.  

 

sfdcfoxsfdcfox
Cookies should always be get or set from ApexPages.currentPage(). Cookies are available to all visualforce pages in the same package.
MTBRiderMTBRider

Ok, thanks.  Based on your information, I went back, took a closer look, and figured out what the problem was.  Turns out, the cookie that page2 was trying to set was never getting set because the page2 window was being closed before it had a chance to set the cookie.

 

---There is a command button on page2 that has an action to set the cookie in the controller method ok() and then close the window:

<apex:page>
   ...
   <apex:commandButton action="{!Ok}" value="Assign" onClick="CloseWindow()"/>

   <script>
      function CloseWindow() {
         var winMain=window.opener;
         if (null==winMain) {
            winMain=window.parent.opener;
         }
         winMain.closePopup();
       }
   </script>
</apex:page>


--Changing the CloseWindow() function to fire on onComplete instead of onClick allowed time for ok() to actually set the cookie...

 ...
   <apex:commandButton action="{!Ok}" value="Assign" onComplete="CloseWindow()"/>
...

 Thanks for your help.

This was selected as the best answer