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 :
1
2
3
4
5
Consider below Apex class :
1
public with sharing class ViewStateController {
2
3
public Integer a {get;set;}
4
private Integer b {get;set;}
5
Transient Integer c {get;set;}
6
private Integer d{get;set;}
7
public Opportunity opp {get;set;}
8
9
public ViewStateController()
10
{
11
a = 20;
12
b = 30;
13
c = 40; //It will not visible in ViewState
14
opp = new Opportunity(name=\’Test\’,StageName=\’Test\’);
15
}
16
//Call this method in Postback
17
public void changeData()
18
{
19
if(a == 20)
20
{
21
a = 2;
22
}
23
if(b == 30)
24
{
25
b = 3;
26
}
27
//As c is declared as Transient, it will not persist the value in Postback
28
if(c == 40)
29
{
30
d = 4;
31
}
32
else{
33
d= 40;//As C value not persisted, else block will execute
34
}
35
}
36
}
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 in Salesforce before Postback
View State in Salesforce before Postback
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.
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:
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 use HTML 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.
Cheers!!