Introduction to View State in Visualforce

Author posted by Jitendra on Posted on under category Categories Apex, Salesforce, Visualforce and tagged as Tags , with 6 Comments on Introduction to View State in Visualforce

Introduction to View State in Visualforce with example and code walk-through

As we know that http protocol is stateless protocol. Unlike Other programming languages like Java and C# the variables, objects are persisted between different method and network calls. However in case of server client model, the server only stores the session related information of client, however it does not stores the temporary data being used by the clients. Sometime hidden field values, IDs and other information are needed by server for processing.

For example : If client needs any data and is loaded by the server in first request, if error comes the server will need to retrieve data again for second request. Also it is possible that few data is created at client side also, so to persist the state of client View State is used in Visualforce.

Before Summer 12, For every form tag the View State was created. However from Summer 12 release only one View State will be created per page. View State is encrypted and transferred to client with each response. View State cannot be viewed with tools like firebug however salesforce has “View State Inspector” to check the View State Data.

Types of Data Saved in View State:
Following data are saved in Viewstate

  1. All public and private data members present in Standard, Custom and Controller extensions.
  2. Objects that are reachable from data members in Custom and Controller extensions.
  3. The component tree for that page that represents the page’s component structure and the associated state which are the values applied to those components.
  4. Small amount of data needed by visualforce for other general purposes.

Transient Variable and Objects:
As Viewstate is transferred over http with every response, it is very necessary to control the size of View State. There are lots of situations where data is not needed after postback or not needed on Visualforce page then in that case we can make variable or object transient. The transient variables are not passed to view state and therefore not stored in View State.

Enable View State Inspector:

  1. View State Inspector will be only visible when developer mode is ON in User profile.
  2. There is option to show the View State in User’s Personal information from where View State inspector can be enabled or disabled.

Sample Walk-through of View State:
Lets take example to demonstrate the behavior of View State.

Visualforce page :

<apex:page Controller="ViewStateController">
<apex:form >
<apex:commandButton value="Click Me" action="{!changeData}"/>
</apex:form>
</apex:page>

Consider below Apex class :

public with sharing class ViewStateController {

public Integer a {get;set;}
private Integer b {get;set;}
Transient Integer c {get;set;}
private Integer d{get;set;}
public Opportunity opp {get;set;}

public ViewStateController()
{
    a = 20;
    b = 30;
    c = 40; //It will not visible in ViewState
    opp = new Opportunity(name='Test',StageName='Test');
}
//Call this method in Postback
public void changeData()
{
    if(a == 20)
    {
        a = 2;
    }
    if(b == 30)
    {
        b = 3;
    }
    //As c is declared as Transient, it will not persist the value in Postback
    if(c == 40)
    {
        d = 4;
    }
    else{
        d= 40;//As C value not persisted, else block will execute
    }
}
}

View State before POST request:
In above example, In constructor i have initialized object of opportunity and variable a,b and c.

  1. As the variable “c” is declared as Transient, it will not be visible in View State.
  2. Variable “b” is private, that means it is not accessible in Visualforce page however it will be available in View State.
  3. variable “d” is not initialized in constructor and hence it is not visible in view state.
  4. The Opportunity object have only those fields which was initialized in code. It does not have other fields present in opportunity.
View State in Salesforce before Postback
View State in Salesforce before Postback

View State After POST request:

  1. As the Variable “c” was declared as Transient, it didn’t persist value.
  2. As per logic when value was assigned in private variable “d”, it started to display in view state inspector.
View State in Salesforce After Postback
View State in Salesforce After Postback

Tips on reducing View State Size:

As we know that total size of ViewState can be 135KB (On date of writing this article), There may be chances that you will hit maximum allowed limit or improve performance of Visualforce page by reducing Viewstate size:

  1. Declare variables as Transient if possible.
  2. Declare variable as Static, as it is not saved in View State.
  3. If you want to manage your own state, instead of using <apex:form> use HTML <form> tag instead.
  4. Recreate state instead of saving in Viewstate. Means if you can use SOQL instead of saving in some object or List, use it.
  5. You can also use Web service call, Ajax Remoting to recreate state instead of saving in Object.

You can also read this article on Salesforce blog which explains ViewState Mechanism.

Related posts

6 thoughts on “Introduction to View State in Visualforce”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.