About Me

My photo
PLANO, Texas, United States

Tuesday, January 12, 2016

View State

  • View State is useful to maintained the state during Post Backs. 
  • As we know GET and Post Request works on HTTP protocol and http protocol is stateless protocol, which means every request and response is treated as independent request for the page. So there should be some mechanism which can be used to mention the state so that we can have the required information for processing the page. View State is the mechanism to maintained the state
  • Suppose we input some value in the VF page and submit the result, If any custom validation runs and throw the error to the VF page, the view state mention the old values that we have filled earlier.
Limitation
In this way, View state makes your job as a developer easier by automatically managing state during post-back. However as we know Salesforce is based on multi-tenant environment where more than one user works at a time so it is required to use the View state within the limit.  135 KB is the limit of View State.
Steps to avoid the View State

  1. Minimize Number of Forms on a Page
  2. Declare variables as Transient if possible.
  3. Use Custom Settings to Store Large Quantities of Read-Only Data
  4. Declare variable as Static, as it is not saved in View State.
  5. Refine your SOQL to only retrieve the data needed by the page.
  6. Refactor Your Pages to Make Its View Stateless: (Instead of using apex:commandLink or apex:commandButton components (which need to be inside a apex:form component) to invoke an action, use an apex:outputLink)
  7. If you want to manage your own state, instead of using <apex:form> use HTML <form> tag instead.

No comments:

Post a Comment