Skip to content

Instantly share code, notes, and snippets.

@doppiomacchiatto
Forked from pchittum/ViewStateStudy_controller
Last active August 29, 2015 14:14
Show Gist options
  • Save doppiomacchiatto/1f6d22af7a1c4f4b817e to your computer and use it in GitHub Desktop.
Save doppiomacchiatto/1f6d22af7a1c4f4b817e to your computer and use it in GitHub Desktop.
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