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
David WrightDavid Wright 

Storing Form Field Values in Cookies

I'm attempting to implement the cookie class in order to store field values from a form on a Sites page. I'm wondering if anybody has done this, because I seem to be confused at how the cookie class works.

 

According to the documentation, cookies can only be created and set in the constructor of the page controller, but no form values are known when the constructor is executed! The limitations this create are pretty substantial--unless I'm understanding it incorrectly. The way I understand it, you can only store values in cookies that are known at the time of page load, which isn't very much... at least not for a page where a user is entering a bunch of data on a form.

 

If anyone has any experience with this, please share...

Best Answer chosen by Admin (Salesforce Developers) 
David WrightDavid Wright

For the sake of others who may have encountered this problem, I figured out a way to make cookies work for storing form values.

 

The trick is to use an inner class to create the cookies. The constructor of the inner class accepts the form values you want to save as parameters and creates a cookie for each of them, then sets all cookies to the page. In the constructor for the page (outer class), you search for the cookies and use them, if found.

 

To set the cookies, simply create an instance of the inner class in whatever action "submits" the form, passing the values you want to save to the constructor.

 

Here's snippets of my code:

 

public with sharing class myControllerExtension {

    public class cookieJar {
       
        public cookieJar(String fName, String lName, String email) {
            Cookie lastName = new Cookie('lName',lName,null,315569260,false);
            Cookie firstName = new Cookie('fName',fname,null,315569260,false);
            Cookie emailA = new Cookie('email',email,null,315569260,false);
            ApexPages.currentPage().setCookies(new Cookie[]{lastName, firstName, emailA});
        }
   
    }//end cookieJar inner class

    public Case myCase {get; set;}
    public String Cfirst {get; set;}
    public String Clast {get; set;}

    public myControllerExtension(ApexPages.StandardController controller) {
        
        myCase = (Case)controller.getRecord();        

        Cookie theCookie = ApexPages.currentPage().getCookies().get('fName');
        if(theCookie != null) Cfirst = theCookie.getValue();
        
        theCookie = ApexPages.currentPage().getCookies().get('lName');
        if(theCookie != null) Clast = theCookie.getValue();
        
        theCookie = ApexPages.currentPage().getCookies().get('email');
        if(theCookie != null) myCase.SuppliedEmail = theCookie.getValue();
    }

      //Submits the form, inserting the record
      public PageReference finish(){
        
        insert myCase;

        cookieJar pageCookies = new cookieJar(Cfirst, Clast, myCase.SuppliedEmail);
        return new PageReference('/apex/PTISupportThankyou');
      }//end finish method

}