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
DannyK88DannyK88 

View state in apex code

Hi Everyone,

I have been doing some work with visualforce pages and I have gotten view state errors every once in a while. I was wondering if there was a way to look for the view state in the apex code to see if certain actions will cause the view state error to happen and stop those actions before they cause the error. I know that tightening my soql code is a good way to manage view state but I was wondering if there was another way.

Thanks,
DannyK88
Best Answer chosen by DannyK88
bob_buzzardbob_buzzard
I'm assuming the errors that you have seen are to do with the viewstate being too large?  Unfortunately you can't get at this information from Apex, only from the Visualforce page itself once you have enabled developer mode.

The viewstate is basically the internal state of your controller, which allows it to be serialised to the page and then deserialised on postback to allow the controller to be recreated to process your request.  You can tune this by not storing as much information in the controller, either by lazy loading (i.e. querying data afresh from the database rather than caching it in a property) or marking properties as transient, which causes it to be removed from the view state.

There's a great article on this at the developerforce site:

https://developer.salesforce.com/page/An_Introduction_to_Visualforce_View_State

All Answers

James LoghryJames Loghry
DannyK88,

Here is an excellent blog post on working with your view state: https://developer.salesforce.com/page/An_Introduction_to_Visualforce_View_State

I
n particular, take a look at using transient variables where possible and keeping your controller as simple as possible, with as little data as possible.  Also, the "Development Mode tab" outlined in the link above, is an excellent tool for examining your view state.
bob_buzzardbob_buzzard
I'm assuming the errors that you have seen are to do with the viewstate being too large?  Unfortunately you can't get at this information from Apex, only from the Visualforce page itself once you have enabled developer mode.

The viewstate is basically the internal state of your controller, which allows it to be serialised to the page and then deserialised on postback to allow the controller to be recreated to process your request.  You can tune this by not storing as much information in the controller, either by lazy loading (i.e. querying data afresh from the database rather than caching it in a property) or marking properties as transient, which causes it to be removed from the view state.

There's a great article on this at the developerforce site:

https://developer.salesforce.com/page/An_Introduction_to_Visualforce_View_State
This was selected as the best answer
DannyK88DannyK88
Thanks for the replies. I think I'm going to try to reduce my query output so that I don't get so many records. That article does hold a lot of useful information thank you.

Thanks,
DannyK88