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

viewstate limit exceeds



I have visualforce page where I have five level search criterias (SearchField1.. SearchField5) and User will enter input for those search fields. On click of search my dynamically generated query will return results and display on grid with pagination. Current page size is 200 and I am getting more than 2500 records for some filter criteria. If my search result returns more than 2500 records then I am getting viewstate limit exceeds error message.


Any idea or recommendation to resolve this?





Mark as much data as you can as transient; transient data is not serialized and therefore won't affect your view state size. You should probably be able to get away with using some variables to track necessary data (i.e. page offset or record IDs, for example), and then return most of the data in transient structures that won't increase your view state size. Just remember that transient data won't be available between requests if it is not submitted back to the server in an input field or parameter, and has to be regenerated each request.

Ankit AroraAnkit Arora

There is nothing to do with pagination, as when you result in collection then it adds in the view state.


To resolve this I have some suggestions for you :


1) Nullify all variables/collections/instance which are no longer in use after once.


Lets say I have a string variable String str = 'Ankit.Arora' ;

Now I am using this string variable to get the list of word after slitting it with "."

List<String> strLst = str.split(',') ;


Now after this I don't need the "str" so I will do like this str = null ;


This is just an simple example, effective on large collection like map,list,set etc.


2) Use transient key word.


You can refer to my blog post also :


Here just to test that your logic can be corrected using the above suggestion try fetching the result not more than 500 for now. Lets say you have total 2500 records after searching, then put a limit forcefully of 500 to check that your logic works after that or you still get the view state error.


Now to track the view state you can enable "View State" in > My Personal Information > check "Development Mode" and "Show View State in development mode" checkbox. After this just hit the page in browser and you can track the view state after each step.




Ankit Arora

Blog | Facebook | Blog Page

Ankit AroraAnkit Arora

Apologies for Cross post.




Ankit Arora

Blog | Facebook | Blog Page