Question is in the subject - is there a way to solve the issue of the browser refresh clearing the viewstate.

The scenario in mind is a multiple Visualforce page form, where the user could potentially be on Step 3 of 5. If the user refreshes and attempts to continue to Step 4, the values entered in Steps 1-3 are lost.

Is there a better solution than some Javascript to catch the refresh action and warn the user/request confirmation that they're starting the form over?

  • The only solution I see to save each step value in database Aug 27, 2018 at 19:57

1 Answer 1


Personally, whenever working on things like this, I have micro services that save the form data back to a temp object in salesforce that holds the form data in a JSON object in a long text field or whatever. I know it sounds complicated, but building this out once usually leaves you with a nice chunk of code to move forward with. Most of my projects have been working with sensitive data, so saving back to the database was also a security requirement vs the alternative Cookie method.

If the form step thing doesn't contain any pieces of data that you wouldn't mind being saved to a users browser cache, you could alternatively write to a cookie and hold onto a nice little mini cache. I usually only do this with smaller, less sensitive forms, but it certainly does the trick if the data saving json thing seems a bit overkill.

  • Using an SObject with JSON data, what's your approach on the lifecycle of that object? I.e. basic method of instantiating, knowing which user corresponds with which record, etc. Aug 28, 2018 at 1:52
  • Usually based off current user ID and joining that to the created by on the sObject record. If one is found during initialization, that data is used and then the object record is updated as a result of user actions. This usually kept a record load of 1 record per user form. Spiced up flavors of this can have types or junctions to “Form ID’s”, different fields for different data chunks, etc etc. You could have a routine to clean up old records after X amount of time so you don’t hold form data for say longer than 30 days. Aug 28, 2018 at 1:58
  • If I'm following along it looks like you have two approaches - one using a generic sObject and one tailored to the form and its specific sObject. For the former, are you doing that in pure HTML and doing the basic client side validation yourself? I.e. by eschewing <apex:inputField> tags you'd need to roll your own datePicker, you'd lose controller side .addError() implementation, etc. Aug 28, 2018 at 2:37
  • I’ve done both approaches, honestly. It’s not hard to grab static input elements by ID using an ends with jquery selector to hold onto the elements and do whatever you want with them in a DOM perspective. Could easily push values since most firms just have get/sets, so you could feed the controller, or you could do everything client side and just use the controller to pass records back as a standardized Json structure and prepopulate that Json object with anything relevant from the server when you load the page. Aug 28, 2018 at 3:12
  • 1
    Also possible as a sessionStorage solution.
    – sfdcfox
    Aug 28, 2018 at 3:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .