View State in Salesforce

Visualforce pages that contain a form component also contain an encrypted, hidden form field that encapsulates the view state of the page. This view state is automatically created, and as its name suggests, it holds the state of the page – state that includes the components, field values and controller state.

To maintain state in a Visualforce page, the platform includes the state of components, field values, and controller state in a hidden form element. This encrypted string is referred to as the view state and has a limit of 135 KB. Large view states require longer processing times for each request, including serializing and de-serializing, and encryption and decryption. By reducing your view state size, your pages can load quicker and stall less often.

In Visualforce, page state is persisted as a hidden form field that is automatically inserted into a form when the page gets generated. We call this the view state of the page. The view state captures the state of the page — state of its associated controllers and extensions and  the component tree on the page. The view state is posted back along with the other form data, which gives the server enough information to recreate the page state to which new changes can be applied.

What View Stage Contained ?

The data in the view state should be sufficient to recreate the state of the page when the postback is received. To do this, it stores the following data:
•  All non-transient data members in the associated controller (either standard or custom) and the controller extensions.
• Objects that are reachable from a non-transient data member in a controller or controller extension.
• The component tree for that page, which represents the page’s component structure and the associated state, which are the values applied to those components.
• A small amount of data for Visualforce to do housekeeping.

VF Page –

<apex:page controller="myController" >
 <apex:form >
 <apex:pageBlock title="My Content" mode="edit">
   <apex:pageBlockButtons >
     <apex:commandButton action="{!save}" value="Save"/>
   <apex:pageBlockSection title="My Content Section" columns="2">
     <apex:outputLabel for="aName">Account Name:</apex:outputLabel>
     <apex:inputText value="{!accountName}"/>

Controller –

public with sharing class myController {

private final Id accountId ;
transient public final String accountName {get; set; }

public myController() {
Account account = [select Id, Name from Account where id = :ApexPages.currentPage().getParameters().get('id')];
accountId = account.Id ;
accountName = account.Name ;

public PageReference save() {
Account myAccount = [select name from Account where id = :accountId]; = accountName ;
update myAccount;
return null;

Examining View State


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s