Skip to content

Instantly share code, notes, and snippets.

@pchittum
Created December 12, 2013 15:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pchittum/7929944 to your computer and use it in GitHub Desktop.
Save pchittum/7929944 to your computer and use it in GitHub Desktop.
How Visualforce passes data between two pages. These are examples supporting a post on salesforce.stackexchange.com regarding how to "save" data when redirecting to another page. My argument was that one solution is to take advantage of view state in Visualforce to pass the data around. http://salesforce.stackexchange.com/questions/21899/starts-…
public with sharing class ViewStateStudy_controller {
public String accId{get;set;}
public Account account{get;set;}
public ViewStateStudy_controller()
{
accId = ApexPages.currentPage().getParameters().get('Id');
if(accId != null)
{
this.account= [ SELECT Name
FROM Account
WHERE Id = : accId ];
}
else
{
this.account= new Account();
}
}
public pageReference save()
{
insert this.account;
return null;
}
public pageReference saveandclose()
{
insert this.account;
//technically this works, but is not a best practice.
PageReference pageRef = new PageReference('/apex/test');
pageRef.setRedirect(true);
return pageRef;
}
public PageReference page2(){
return Page.ViewStateStudy_page2;
}
public PageReference page2byGet(){
//this way will force initial page request by GET thus dumping view state
//usually this would be used to reset or cancel a set of page flows.
PageReference pageRef = Page.ViewStateStudy_page2;
pageRef.setRedirect(true);
return pageRef;
}
public PageReference page1(){
return Page.ViewStateStudy_page1;
}
}
<apex:page controller="ViewStateStudy_controller">
<apex:form >
<apex:actionRegion >
<apex:pageBlock id="pbAccountDetails">
<apex:pageBlockSection columns="1" collapsible="false">
<apex:inputField value="{!account.Name}" required="true" />
<!--<apex:inputfield value="{!account.Email__c}" required="true"/>-->
</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value="Save"/>
<apex:commandButton action="{!saveandclose}" value="Save and Close" immediate="true"/>
<apex:commandButton action="{!page2}" value="Next"/>
<apex:commandButton action="{!page2byGet}" value="Next by Get"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:actionRegion>
</apex:form>
</apex:page>
<apex:page controller="ViewStateStudy_controller" >
<apex:form >
<apex:pageBlock >
<apex:pageBlockButtons >
<apex:commandButton action="{!page1}" value="Previous" />
</apex:pageBlockButtons>
<apex:pageBlockSection >
<apex:outputField value="{!account.Name}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment