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
eliotstock2eliotstock2 

session scope

Hi there,

 

I have a series of Visualforce pages with forms that need to be filled out sequentially. Suppose on page 2 in this flow, I need access to a custom SObject that I just created on page 1. Except that I never actually stuck it in the DB with 'insert' on page 1, I just construted it.

 

In a Java web app, I'd put my instance from page 1 in the session and get at it from page 2. Is there any such thing in Salesforce?

 

If not, do I have to either put my custom SObject in the DB after page 1 (before I'd like to), just so I can get it out again on page 2? Or perhaps put a huge set of hidden form fields from page 1 on page 2 and have the controller populate these?

 

Thanks in advance.

 

Eliot Stock.

Best Answer chosen by Admin (Salesforce Developers) 
jwetzlerjwetzler

Or you can just create a wizard in Visualforce.  If all of your pages use the same controller and you forward to them rather than redirect, all of your pages will share the same instance of the controller, therefore maintaining your state from page to page.

 

See this example in the dev guide.

All Answers

sfdcfoxsfdcfox
The Visualforce way of doing it would be to use actionRegions to render part of the page at once. When they click "next", it would appear to them that they've gone to a new page, but in reality are on the same page with a different section of it rendered. If you don't save data to the DB, you can't really pass around a large number of parameters in a session (you can use page parameters, but that's typically a 3kb limit). You could also take a look at the apex:tabPanel and apex:tab components to create a tabbed interface (similiar to tabs in Microsoft Windows, when you go to some complex option screens).
jwetzlerjwetzler

Or you can just create a wizard in Visualforce.  If all of your pages use the same controller and you forward to them rather than redirect, all of your pages will share the same instance of the controller, therefore maintaining your state from page to page.

 

See this example in the dev guide.

This was selected as the best answer
eliotstock2eliotstock2

Thanks! I was already doing exactly what the wizard example in the docs does, except that I was adding all these hidden fields to pages 2 onwards for stuff entered on earlier pages. I hadn't realised I don't need to do that.

 

How does it work? Does the server keep an instance of my controller alive in memory between requests or does the state get written to an internal hidden form field on the client for every page view? I think the latter.

eliotstock2eliotstock2
Thanks - I wasn't aware of the 3KB limit. I think I should come in well under this for my data though, so the wizard approach will work for me.
eliotstock2eliotstock2
Seems I spoke too soon. I don't need hidden form fields for standard SObjects but for my own SObject, and for another Apex class which is not an SObject, I do need them. Otherwise the fields on these get lost from one page to the next.
eliotstock2eliotstock2
In fact I'm now confused about the lifecycle of a custom controller. Does the constructor get called on every request?