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
- All public and private data members present in Standard, Custom and Controller extensions.
- Objects that are reachable from data members in Custom and Controller extensions.
- 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.
- 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:
- View State Inspector will be only visible when developer mode is ON in User profile.
- 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.
- As the variable “c” is declared as Transient, it will not be visible in View State.
- Variable “b” is private, that means it is not accessible in Visualforce page however it will be available in View State.
- variable “d” is not initialized in constructor and hence it is not visible in view state.
- The Opportunity object have only those fields which was initialized in code. It does not have other fields present in opportunity.
View State After POST request:
- As the Variable “c” was declared as Transient, it didn’t persist value.
- As per logic when value was assigned in private variable “d”, it started to display in view state inspector.
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:
- Declare variables as Transient if possible.
- Declare variable as Static, as it is not saved in View State.
- If you want to manage your own state, instead of using <apex:form> use HTML <form> tag instead.
- Recreate state instead of saving in Viewstate. Means if you can use SOQL instead of saving in some object or List, use it.
- 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.
Leave a Reply