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

Collecting Common Fields in Cookies

I'm trying to collect common fields from a webform in a cookie so that I can auto-fill them for my users when they visit our webform multiple times. For example if they've visited the page and filled out the form in the last 3 years I want it to auto-fill the First Name, Last Name, Email, Phone, and Extension fields for them. 


This page was previously written with Javascript, and I'd like to translate it into an apex controller instead.


My issue lies in that the values for cookies can only be set in constructors, I don't now how to collect the data after the page has been loaded and the constructor called.


I received some vague advice from a coworker about using page references but it didn't help much.


Any help would be much appreciated!


Is this web form hosted on SF Sites? Or how was this web form created? Is it standard web to lead?


It will be hosted on sites, and it's a web to case form


Hmm, there's a Cookie class that you would want to use for this, thought I'm not sure how to use it:


This is assuming that the Case-creation form was created in VF. Just talking my way through this, there would be 2 solutions:

1) Store the user's info in the SF database, tied to a key. Tell the browser to store this key in a cookie. The next time the browser requests this page, it will give you this cookie. Take the key from the cookie, and retrieve the user's data from SF again. Then push the data into the correct form fields to display to the user on the VF page.


2) Create a cookie in the user's browser for each field's value. This will not use the SF database. When the browser hits your VF page, have your code check for any cookie info that the browser sent to you on the GET request. Take any data that was in the cookie, and push it back into the form fields on the VF page.


I'm not sure which is considered best practice, but these are the solutions that I would pursue.


The problem I'm running into is how to instantiate the cookies so that I can call the setCookies() method in such a way that it stores the cookies with the correct value ( I'm speaking of the second option you mentioned).


If I can only deal with them in the constructor how can I collect the fields once they've been added?