Salesforce Visualforce pages use a view state mechanism to persist variable values between HTTP requests. Because HTTP is a stateless protocol, Salesforce serializes all public and private instance variables in a Visualforce controller into a hidden view state field that is sent to the browser and returned with each POST request.
The maximum allowed view state size is 170KB. When this limit is exceeded, internal users see the error:
"Maximum view state size limit (170KB) exceeded. Actual view state size for this page was \KB."
External users (such as Community members or Site Guest Users) may instead see a generic "Error while loading a VF page" message. If this error occurs intermittently for specific records, it is likely a view state size issue. Contact Salesforce Developer Support and request confirmation by reviewing server logs.
How Visualforce View State Works
When a Visualforce page with a controller loads, Salesforce executes the following sequence:
When the user clicks a button that triggers a POST request (such as an apex:commandButton):
How to Reduce View State Size
Use the following techniques to reduce view state size:
1. Use the transient Keyword Declare instance variables as transient when their values do not need to persist across requests. Transient variables are recalculated on each page load and are not serialized into the view state. For example, a DateTime variable declared as transient is recalculated on each page refresh, while a non-transient DateTime variable retains its value across requests via the view state.
2. Minimize the Number of Forms Minimize the number of apex:form tags on a page. Use apex:actionRegion to limit the portion of the page submitted in a POST request instead of using multiple forms.
3. Refine SOQL Queries Retrieve only the fields and records needed for the page. Avoid querying large collections of records or unnecessary fields that contribute to controller variable size.
4. Use Transient Variables for Large Collections All public and private instance variables in standard controllers, custom controllers, and controller extensions are included in the view state. Use transient on large collection variables (Lists, Maps, Sets) that can be re-queried on each request.
5. Replace Command Links and Buttons Where Possible Where navigation is the goal rather than a server-side action, use apex:outputLink instead of apex:commandLink or apex:commandButton, as output links do not trigger a full view state submission.
Additional Resources :
Optimizing the view state best practice : https://help.salesforce.com/s/articleView?id=000386042&type=1
View State Tab https://help.salesforce.com/s/articleView?id=platform.code_dev_console_tab_view_state.htm&type=5
Visualforce Basics https://trailhead.salesforce.com/content/learn/modules/visualforce_fundamentals
000384480

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.