+ Start a Discussion
sai tarunsai tarun 

how do we reduce size of the view state when view state error occurs.......????

how do we reduce size of the view state when we got view state error??????
please tell me in detail , with example if possible......
Thanks in advance........
Best Answer chosen by sai tarun
NagendraNagendra (Salesforce Developers) 
Hi Sai,

As its name suggests, it holds the state of the page - state that includes the components, field values, and controller 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:
  1. All non-transient data members in the associated controller (either standard or custom) and the controller extensions.
  2. Objects that are reachable from a non-transient data member in a controller or controller extension.
  3. 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.
  4. A small amount of data for Visualforce to do housekeeping.

Developer environments have a view state inspector, which lets you view the contents of the view state. This information can help you in optimizing the view state size. Once enabled, it shows up as a tab in Development Mode, as follows. you can enable that from 

Edit User-> Select view state checkbox after development mode checkbox.
User-added image
Check your view state as shown above

Here are some quick tips which will help you to reduce view state size without using transient keyword
  1.  If your view state is affected by a large component tree, try reducing the number of components your page depends on.
  2. Avoid SOQL queries in your Apex controller getter methods.
  3. Use the private setter for elements where you are not updating value from the visualforce page.
  4. Decrease collection items if they are not going to be used in the page.
  5. Define private if they are not used on a visualforce page.
  6. If you notice that a large percentage of your view state comes from objects used in controllers or controller extensions, consider refining your SOQL calls to return only data that's relevant to the Visualforce page.

For more information please check with below link. Hope this helps.

Please mark this as solved if the information helps so that it gets removed from the unanswered queue which results in helping others who are encountering a similar issue.

Thanks,
Nagendra