+ Start a Discussion
Ryley Oroku 10Ryley Oroku 10 

apex:form tag breaks the view state limit

Hi all,

I seem to be having a problem with <apex:form>. When I run my visualforce page without out it, my page has no problem loading. However, when I add that tag, it get the view state limit reached error message. Even just putting <apex:form></apex:form> with nothing in between causes the error. Here is my code. 
<apex:page controller="VisitReportPrototypeController">
  <apex:pageBlock>
    <apex:form>
      <apex:outputText value="Select a term: "/>
      <apex:selectList value="{!selectedTermNumber}" multiselect="false" size="1">
        <apex:selectOptions value="{!termOptions}"/>
      </apex:selectList>
    </apex:form>
    <apex:pageBlockTable value="{!group1Rows}" var="row">
      <apex:column value="{!group1[row]}" headerValue="Totals" style="width:150px"/>
      <apex:column value="{!total[row]}" style="width:150px"/>
      <apex:column value="{!totalsFirstYear[row]}" style="width:150px"/>
      <apex:column value="{!totalsTransfer[row]}" style="width:150px"/>
    </apex:pageBlockTable>
    <br />
    <apex:pageBlockTable value="{!group1Rows}" var="row">
      <apex:column value="{!group1[row]}" headerValue="Visited" style="width:150px"/>
      <apex:column value="{!visited[row]}" style="width:150px"/>
      <apex:column value="{!visitedFirstYear[row]}" style="width:150px"/>
      <apex:column value="{!visitedTransfer[row]}" style="width:150px"/>
    </apex:pageBlockTable>
    <br />
    <apex:pageBlockTable value="{!group1Rows}" var="row">
      <apex:column value="{!group1[row]}" headerValue="Not Visited" style="width:150px"/>
      <apex:column value="{!didNotVisit[row]}" style="width:150px"/>
      <apex:column value="{!notVisitedFirstYear[row]}" style="width:150px"/>
      <apex:column value="{!notVisitedTransfer[row]}" style="width:150px"/>
    </apex:pageBlockTable>
    <br />
    <apex:pageBlockTable value="{!group2Rows}" var="row">
      <apex:column value="{!group2[row]}" headerValue="Summary" style="width:150px"/>
      <apex:column value="{!summaryVisited[row]}" style="width:150px"/>
      <apex:column value="{!outOf[row]}" style="width:150px"/>
      <apex:column value="{!percentage[row]}" style="width:150px"/>
    </apex:pageBlockTable>
  </apex:pageBlock>
</apex:page>
User-added image
Also, you can see that the viewstate size is way under the limit. Could anyone tell me why I might be getting this error?
Thanks.
Best Answer chosen by Ryley Oroku 10
Ryley Oroku 10Ryley Oroku 10
So after some tinkering I figured out was was going on. Basically I had variables that were referencing big queries, and they were being transmitted through the view state, causing the page to hit the size limit. I put the "Transient" keyword before the the variable names referencing the queries so they wouldn't be transmitted anymore. Now everything works fine.

All Answers

SalesFORCE_enFORCErSalesFORCE_enFORCEr
<apex:form> contributes to page view state. That's why Salesforce recommends not to use multiple forms inside a page or better not to use a form, if possible.
Ryley Oroku 10Ryley Oroku 10
Thanks for the reply SalesFORCE. I understand that it contributes to the page view state. However, I'm only using one form and I'm pretty sure one form with nothing it it will make the page view state size go from 7kb to over 135kb.
Ryley Oroku 10Ryley Oroku 10
So after some tinkering I figured out was was going on. Basically I had variables that were referencing big queries, and they were being transmitted through the view state, causing the page to hit the size limit. I put the "Transient" keyword before the the variable names referencing the queries so they wouldn't be transmitted anymore. Now everything works fine.
This was selected as the best answer