Think. Build. Salesforce Solutions.

Salesforce Consulting Services across Salesforce Clouds & Across App Lifecycles

Blog

What is view state error in Salesforce

By |2020-07-15T08:19:57+00:00August 23rd, 2016|Tags: , , , , |

View State:  As the name suggests, it is an error of View State. Maximum view state size limit (135KB) exceeded is the error message pertaining to View State error and here follows a brief detailing of the issue.

View state holds the state/size of the visual force page that includes the components, field values, and controller state. This is important in the light that salesforce provides only a standard size of 135kb for any individual page. If the size of a particular page exceeds 135kb, the page will throw a view state error.

view state error in salesforce
A screenshot showing a view state error in salesforce

To check the view state of a visual force page you have to make sure that Show View State in Development Mode checkbox in the User Layout is checked.

You can access this setting from: Setup-> Manage User-> User-> Edit (on a particular user)-> Show View State in Development Mode

view state error

A screenshot showing a view state of a Visual Force page in Salesforce

How to reduce View State:

1. Use the transient keyword: The simplest and the most logical way is to keep the size of the VF page as small as possible. For this purpose, Transient Variables are used as these do not store the values permanently, these variables just hold the values temporarily and therefore not stored in View State. Declaring variables as transient reduces view state size.

An example showing how a transient variable is declared:
transient String name;

2. Refine your SOQL: Reduce the number of records displayed on a page by refining SOQL. The use of AND statements, WHERE clause in SOQL, removing NULL results can help assure that only required data comes back from SOQL.

3. Minimize the Number of Forms on a Page: We should use <apex:actionReagion> instead of using two <apex:forms>.

Each form on your page will have its own copy of view state. Instead of having multiple forms on a page, have a single form and use <apex:actionRegion> to submit portions of the form.

This will make sure only a single copy of the view state is associated with that VF page instead of two copies.

Eg:

// Using two <apex:form>

<apex:page controller=”ApexController”>

<apex:form>

<apex:commandButton action=”{!objectFirstMethod}” value=”First Object”/> </apex:form>

<apex:form>

<apex:commandButton action=”{!objectSecondMethod}” value=”Second Object”/>

 </apex:form>

</apex:page>

// Using <apex:actionRegion>

<apex:page controller=”ApexController”>

<apex:form>

<apex:commandButton action=”{!objectFirstMethod}” value=”First Object”/>

<apex:actionRegion>

<apex:commandButton action=”{!objectSecondMethod}” value=”Second Object”/>

</apex:actionRegion>

</apex:form>

</apex:page>

4. Use Custom Setting: Use custom setting to store large quantities of read-only data instead of custom objects. Accessing custom settings is faster than accessing custom objects since custom settings are part of application’s cache and does not require a database query to retrieve the data unlike custom objects.

5. Use <apex:outputLink> : Instead of using <apex:commandLink> or <apex:commandButton> components, Use <apex:outputLink> because <apex:commandLink> or <apex:commandButton> requires to be included inside a <apex:form> component unlike <apex:outputLink>.

Leave A Comment