function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
renu anamalla 9renu anamalla 9 

What is the view state?and error?ang how to resolve the problem?

What is the view state?and error?ang how to resolve the problem?
nagendra 6989nagendra 6989
Hi Renu,

As you wanted to understand about view state error, please find the below explanation.........

View State : 

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.

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 is Contained in the View State:

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:

a)All non-transient data members in the associated controller (either standard or custom) and the controller extensions.
b)Objects that are reachable from a non-transient data member in a controller or controller extension.
c)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.
d)A small amount of data for Visualforce to do housekeeping.

View state data is encrypted and cannot be viewed with tools like Firebug. The view state inspector described below lets you look at the contents of view state.

Best Practices for Optimizing View State:

a)Minimize Number of Forms on a Page
b)Declare Variables as Transient to Reduce View State
c)Recreate State versus Storing It in View State
d)Use Custom Objects or Custom Settings to Store Large Quantities of Read-Only Data
e)Refine Your SOQL to Retrieve Only the Data Needed by the Page
f)Refactor Your Pages to Make Its View Stateless
g)Consider Doing Your Own State Management in Certain Cases

Please let me know if this helps you ...........................

Thanks & Regards,
sandhya reddy 10sandhya reddy 10
Hi Renu anamalla,

Please see below links

I have some points which will help you better understand.
The use of transient keyword will allow you to minimize the Viewstate. Though, technically there is no known way to calculate Viewstate in visualforce via Salesforce Apex API, we just need to code within the best practices boundaries and that usually make sure that Viewstate issues never arise,

Best Practices for view state.
Salesforce's MVC is much different than ASP.NET in that with Salesforce, controllers are used with VisualForce pages which relies on a View State which is limited to 135 kb. So, what to do in a particular situation greatly depends on the View State of your VisualForce page; something which can definitely be affected by your controller. A large or poorly written controller can slow down a VisualForce page and load up the View State.
The things which primarily affect the View State from a VF standpoint are going to be the DOM component tree and it's associated state. The heaviest impact on those will be the following Apex components:
apex input
apex command
apex action
apex select
apex in-line edit support
Any data sent to the controller through getters or returned by setters will also be held in the view state. If the data is only needed for the duration of the current page request, one will want to use the transient key word on those variables in the controller if it all possible. Otherwise, they get held in the controller and thus in the view state as well. Now, imagine a situation where you have a large controller and the same variable gets passed to a number of methods which have other variables they're assigned to which might be returned to the page. The memory required to hold the contents of the controller will increase as will the resulting view state. In such a situation, having the added functionality for re-use would not be a good thing.
That leads me to where I'm going with this. The "best practice" for fast loading VF pages would be to use controller extensions to add functionality to existing "base" controllers as needed. That would keep them as light weight as possible; particularly where view state and page load times start to become an issue.

If u want to learn something more advance in vf please see below link which has video

Please let us know if this helps you.

Thanks and Regards