anusha(salesforce developer)

Thursday 19 January 2017

what is View State and how can we avoid these View State error?

View State:  As the name suggests, 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.
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
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>

1 comment:

  1. Hi there, i read your blog occasionally and i own a similar one and i was just curious if you get a lot of spam responses? If so how do you reduce it, any plugin or anything you can recommend? I get so much lately it's driving me insane so any assistance is very much appreciated.college writing skills test bank & solutions manual

    ReplyDelete