Monday, August 21, 2017

How To Reduce Visualforce’s View State

View State is a technique Salesforce uses to maintain the current state of the page and the data that was queried between the user requesting things from the server and sending any changes back to the server.If the view state gets too large problems can begin to occur. The view state is an encrypted field that contains the following data:
  • All of the non-transient data that is in the associated controller and any associated controller extensions
  • State related to all of the form elements and components that are used on the page.
  • Finally there’s some sort of internal state that Salesforce doesn’t provide much information about.
In Salesforce, the view state needs to be less than 135kb or you will get an exception.The error message will look something like this and be displayed to the end user: “Maximum view state size limit (135KB) exceeded. Actual view state size for this page was 227.239KB ”

Why View State Matters

As developers, we need to keep the view state low because it really effects page performance. The reason that the view state is so important is that because every time the page is re-rendered for some reason or that a request has been sent to the server that we need to send and receive the view state. As you can imagine, a large view state being constantly transmitted would add a lot of overhead.

Understanding what the Problem is

It’s easy to spend a lot of time doing unnecessary optimizations that will make future changes to the page and any associated apex more difficult. This means that the first step to reducing the view state when it is consistently being exceeded is to try and understand what the problem actually is. As the view state is encrypted, you can’t simply use the Chrome Developer Tools to see what’s being stored, instead you need to make some minor configuration changes to Salesforce.
The configuration change is to temporarily enable development mode for your user, or the user you will be testing with, and then open up the developer console. A new tab should be opened in the developer console that is called “view state inspector”.

Use the Transient Keyword

Use the Transient keyword to reduce the amount of data that needs to be persisted between the server and user. Transient variables are instance variables that aren’t able to be saved. Keep in mind that static variables are automatically transient and aren’t transmitted. There are some apex objects that are automatically transient, most of these are the objects created by System methods.

Return only the Minimum Data needed

Modify SOQL queries to only need the fields and objects that are actually being used by the visualforce page.

Refactor and Simplify the page

Minimize the number of form elements and visualforce components that are on the page. In almost every case that I have had issues with the view state being too large interally, the problem has been related to apex:repeat components and the apex components that are nested inside. Apex:outputpanels also generate a significant amount of viewstate that can be fairly easily replicated with plain html.

No comments:

Post a Comment