You need to sign in to do that
Don't have an account?
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
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
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
Here is an excellent blog post on working with your view state: https://developer.salesforce.com/page/An_Introduction_to_Visualforce_View_State
In 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.
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
Thanks,
DannyK88