Wizard:
Salesforce wizard is a good example to understand the view state.Please check the view state once you done.You can get UtilityJS
Here
public class JobAppWizard {
// These properties will be used to maintain state
// Search results are not transient in case the user accidentally selects
//
the wrong one and wants to go back
public List<Candidate__c> results {
get{
if(results == null) results = new List<Candidate__c>();
return results;
}
set;
}
public ApexPages.standardController controller {get; set;}
public String searchText {
get;
set{
value = value.endsWith('*') ? value : value + '*';
searchText = value;
}
}
public Boolean showSearch {
get{
if (showSearch == null) showSearch = true;
return showSearch;
}
set;
}
public Job_Application__c jobApplication { get; set; }
public Candidate__c candidate {
get{
if (candidate == null) candidate = new Candidate__c();
return candidate;
}
set;
}
public ID candidateId { get; set; }
public JobAppWizard(ApexPages.StandardController stdController) {
// constructor
controller = stdController;
this.jobApplication = (Job_Application__c)stdController.getRecord();
if ((ApexPages.currentPage().getParameters().get('posId') != null)&&
(ApexPages.currentPage().getParameters().get('posId') != '')){
jobApplication.Position__c = ApexPages.currentPage().getParameters().get('posId');
}
}
// The next 3 methods control navigation through
// the wizard. Each returns a PageReference for one of the 3 pages
// in the wizard. Note that the redirect attribute does not need to
// be set on the PageReference because the URL does not need to change
// when users move from page to page.
public PageReference step1() {
return Page.jobAppStep1;
}
public PageReference step2() {
return Page.jobAppStep2;
}
public PageReference step3() {
if (candidate.last_name__c == null){
candidate.last_name__c.addError('Last Name is required');
return null;
} else {
return Page.jobAppStep3;
}
}
// this function is called by step2 & step3 pages in the action attribute,
//
in case anyone tries to go directly to them it will navigate the user back to step1
public PageReference checkPosition(){
if (jobApplication.Position__c == null) {
PageReference newRef = Page.jobAppStep1;
newRef.setRedirect(true);
return newRef;
} else {
return null;
}
}
public PageReference doSearch() {
results = (List<Candidate__c>)[FIND :searchText IN ALL FIELDS RETURNING Candidate__c(Id, First_Name__c, Last_Name__c, Email__c, City__c, State_Province__c, Phone__c)] [0];
return null;
}
// doing a separate query here to keep the view state down
public PageReference SelectCandidate() {
candidate = [select id,first_name__c,last_name__c,phone__c,email__c,mobile__c, street_address_1__c,street_address_2__c,city__c,state_province__c,zip_postal_code__c,country__c from Candidate__c where id=:candidateId];
return Page.jobAppStep3;
}
public PageReference NewCandidate(){
if (!showSearch) {
candidate = new Candidate__c();
candidateId = null; // reset the id so we know what to do upon save
}
return null;
}
public PageReference save() {
// catches need to return null; redo using ApexPages.addMessages(e) & <apex:pageMessages>
if (candidateId == null) {
// it's a brand spanking new candidate so we need to insert it first
try{
insert candidate;
System.debug('new candidate=' + candidate);
} catch (System.DmlException e) {
ApexPages.addMessages(e);
return null;
}
}
System.debug('jobApplication=' + jobApplication);
jobApplication.Candidate__c = candidate.id;
try{
insert jobApplication;
} catch (System.DmlException e) {
ApexPages.addMessages(e);
return null;
}
controller = new ApexPages.standardController(jobApplication);
return controller.view();
}
}
and the step 1 page looks like this
<apex:page standardController="Job_Application__c" extensions="JobAppWizard" tabStyle="Job_Application__c">
<apex:includeScript value="{!$Resource.utilityJS}" />
<apex:sectionHeader title="Job Application" subtitle="Step 1 of 3"/>
<apex:form >
<apex:pageBlock id="theBlock" title="Job Application Edit" mode="edit">
<apex:pageBlockButtons >
<apex:commandButton action="{!step2}" value="Next"/>
<apex:commandButton action="{!cancel}" value="Cancel" onclick="return confirmCancel()" immediate="true" style="margin-left: 2em"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Information" columns="1">
<apex:inputField value="{!JobApplication.Status__c}"/>
<apex:inputField value="{!JobApplication.Stage__c}"/>
<apex:inputField value="{!JobApplication.Position__c}" required="true"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Step 2 page :
<apex:page standardController="Job_Application__c" extensions="JobAppWizard" tabStyle="Job_Application__c" action="{!checkPosition}">
<apex:includeScript value="{!$Resource.utilityJS}" />
<apex:sectionHeader title="Job Application" subtitle="Step 2 of 3"/>
<apex:form id="theForm" >
<apex:pageBlock id="theRadioBlock" title="Choose" mode="edit">
<apex:pageMessages />
<apex:pageBlockButtons location="top" >
<apex:commandButton action="{!step1}" value="Previous"/>
<apex:commandButton action="{!step3}" value="Next" rendered="{!NOT(ShowSearch)}"/>
<apex:commandButton action="{!cancel}" value="Cancel" onclick="return confirmCancel()" immediate="true" style="margin-left: 2em"/>
</apex:pageBlockButtons>
<apex:pageBlockSection collapsible="false" title="Search or New" id="searchOrNew" columns="1">
<apex:selectRadio value="{!ShowSearch}">
<apex:selectOption itemValue="true" itemLabel="Search for Existing Candidate"/>
<apex:selectOption itemValue="false" itemLabel="Create New Candidate" />
<apex:actionSupport rerender="theForm" event="onclick" action="{!NewCandidate}"/>
</apex:selectRadio>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:pageBlock id="theSearchBlock" title="Specify Candidate" mode="edit" rendered="{!ShowSearch}">
<apex:pageBlockButtons location="bottom" >
<apex:commandButton action="{!step1}" value="Previous"/>
<apex:commandButton action="{!step3}" value="Next" rendered="{!NOT(ShowSearch)}"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Candidate Search" collapsible="false" id="candSearch" columns="1">
<apex:outputPanel layout="searchBlock" style="padding:5px 0px;margin-bottom:5px;background-color:#D9D9D9">
<apex:outputLabel style="padding:0px 5px;font-size:95%;font-weight:bold;font-family: 'Verdana','Geneva',sans-serif;" value="Enter Candidate Last Name" for="searchText"/>
<apex:inputText id="searchText" value="{!searchText}" size="50" />
<apex:commandButton value="Search" action="{!doSearch}" rerender="results" status="status"/>
<apex:actionStatus id="status" startText="requesting..."/>
</apex:outputPanel>
</apex:pageBlockSection>
<apex:pageBlockSection title="Candidate Search Results" id="results" columns="1" collapsible="false" >
<apex:pageBlockTable value="{!results}" var="c" id="thePBTable" >
<apex:column headerValue="Action">
<apex:commandLink action="{!SelectCandidate}" value="Select" >
<apex:param name="a" assignTo="{!candidateId}" value="{!c.Id}"/>
</apex:commandLink>
</apex:column>
<apex:column value="{!c.First_Name__c}"/>
<apex:column value="{!c.Last_Name__c}"/>
<apex:column value="{!c.Email__c}"/>
<apex:column value="{!c.Phone__c}"/>
<apex:column value="{!c.City__c}"/>
<apex:column value="{!c.State_Province__c}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:pageBlock id="theCandidateBlock" title="New Candidate" mode="edit" rendered="{!NOT(ShowSearch)}">
<apex:pageBlockButtons location="bottom" >
<apex:commandButton action="{!step1}" value="Previous"/>
<apex:commandButton action="{!step3}" value="Next"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Candidate Information" columns="2" id="candInfo" collapsible="false">
<apex:inputField value="{!Candidate.First_Name__c}"/>
<apex:inputField value="{!Candidate.Phone__c}"/>
<apex:inputField value="{!Candidate.Last_Name__c}" />
<apex:inputField value="{!Candidate.Mobile__c}"/>
<apex:pageBlockSectionItem >
<apex:outputText value=""/>
</apex:pageBlockSectionItem>
<apex:inputField value="{!Candidate.Email__c}" />
</apex:pageBlockSection>
<apex:pageBlockSection title="Address Info" columns="1">
<apex:inputField value="{!Candidate.Street_Address_1__c}"/>
<apex:inputField value="{!Candidate.Street_Address_2__c}" />
<apex:inputField value="{!Candidate.City__c}"/>
<apex:inputField value="{!Candidate.State_Province__c}"/>
<apex:inputField value="{!Candidate.Zip_Postal_Code__c}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Step 3 Page:
<apex:page standardController="Job_Application__c" extensions="JobAppWizard" tabStyle="Job_Application__c" action="{!checkPosition}">
<apex:includeScript value="{!$Resource.utilityJS}" />
<apex:sectionHeader title="Job Application" subtitle="Step 3 of 3"/>
<apex:form >
<apex:pageBlock id="theBlock" title="Job Application and Candidate" mode="detail">
<apex:pageBlockButtons >
<apex:commandButton action="{!step2}" value="Previous"/>
<apex:commandButton action="{!save}" value="Save"/>
<apex:commandButton action="{!cancel}" value="Cancel" onclick="return confirmCancel()" immediate="true" style="margin-left: 2em"/>
</apex:pageBlockButtons>
<apex:pageMessages />
<apex:pageBlockSection title="Job Application" columns="1" collapsible="false">
<apex:outputField value="{!JobApplication.Status__c}"/>
<apex:outputField value="{!JobApplication.Stage__c}"/>
<apex:outputField value="{!JobApplication.Position__c}"/>
</apex:pageBlockSection>
<apex:pageBlockSection title="Candidate Information" columns="2" id="candInfo" collapsible="false">
<apex:outputField value="{!Candidate.First_Name__c}"/>
<apex:outputField value="{!Candidate.Phone__c}"/>
<apex:outputField value="{!Candidate.Last_Name__c}" />
<apex:outputField value="{!Candidate.Mobile__c}"/>
<apex:pageBlockSectionItem >
<apex:outputText value=""/>
</apex:pageBlockSectionItem>
<apex:outputField value="{!Candidate.Email__c}" />
<apex:pageBlockSectionItem >
<apex:outputText value=""/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageBlockSection showHeader="false" columns="1">
<apex:outputField value="{!Candidate.Street_Address_1__c}"/>
<apex:outputField value="{!Candidate.Street_Address_2__c}" />
<apex:outputField value="{!Candidate.City__c}"/>
<apex:outputField value="{!Candidate.State_Province__c}"/>
<apex:outputField value="{!Candidate.Zip_Postal_Code__c}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>