0

Following is code in which I get view state error.

Visualforce Page

<apex:page Controller="ExportEmpDataController" contenttype="application/vnd.ms-excel#EmploymentData.xls">

  <apex:form>
    <apex:pageBlock >
        <apex:pageBlockTable value="{!EmpDataList}" var="emp" border="1">

           <apex:column value="{!emp.Name}"/>
           <apex:column value="{!emp.County__c}"/>           
           <apex:column value="{!emp.JanEmp__c}"/>           
           <apex:column value="{!emp.FebEmp__c}"/>           
           <apex:column value="{!emp.MarEmp__c}"/>           
           <apex:column value="{!emp.AprEmp__c}"/>           
           <apex:column value="{!emp.MayEmp__c}"/>                                     
           <apex:column value="{!emp.JunEmp__c}"/>           
           <apex:column value="{!emp.JulEmp__c}"/>                                                          

        </apex:pageBlockTable>        

    </apex:pageBlock>
  </apex:form>
</apex:page>

Controller

public with sharing class ExportEmpDataController {

    public List<Employment_Data__c> EmpDataList{get;set;}    

    public ExportEmpDataController (){             

        EmpDataList = [Select id, AprEmp__c,County__c,JanEmp__c,FebEmp__c,MarEmp__c,AugEmp__c,DecEmp__c,
        JulEmp__c,JunEmp__c,MayEmp__c, NovEmp__c, OctEmp__c, SepEmp__c, Total__c, Name, AvgEmp__c
        from Employment_Data__c ORDER BY Name ASC];

    }                           
}

Thanks, Vml

3 Answers 3

0

Use readonly="true" and use private set and transient for EmpDataList

This will reduce your view state..

<apex:page Controller="ExportEmpDataController" contenttype="application/vnd.ms-excel#EmploymentData.xls" readonly="true">

  <apex:form>
    <apex:pageBlock >
        <apex:pageBlockTable value="{!EmpDataList}" var="emp" border="1">

           <apex:column value="{!emp.Name}"/>
           <apex:column value="{!emp.County__c}"/>           
           <apex:column value="{!emp.JanEmp__c}"/>           
           <apex:column value="{!emp.FebEmp__c}"/>           
           <apex:column value="{!emp.MarEmp__c}"/>           
           <apex:column value="{!emp.AprEmp__c}"/>           
           <apex:column value="{!emp.MayEmp__c}"/>                                     
           <apex:column value="{!emp.JunEmp__c}"/>           
           <apex:column value="{!emp.JulEmp__c}"/>                                                          

        </apex:pageBlockTable>        

    </apex:pageBlock>
  </apex:form>
</apex:page>

Class

public with sharing class ExportEmpDataController {

    public transient List<Employment_Data__c> EmpDataList{get; private set;}    

    public ExportEmpDataController (){             

        EmpDataList = [Select id, AprEmp__c,County__c,JanEmp__c,FebEmp__c,MarEmp__c,AugEmp__c,DecEmp__c,
        JulEmp__c,JunEmp__c,MayEmp__c, NovEmp__c, OctEmp__c, SepEmp__c, Total__c, Name, AvgEmp__c
        from Employment_Data__c ORDER BY Name ASC];

    }                           
}
0
0

Use repeat tag to loop the list and replace column tag html table tag.

1
  • I changed my code with html code. Its also working for me. Thanks Nagachandra.
    – VML
    Jan 19, 2016 at 8:16
0

You should filter out results in your SOQL query as it is a best practices and would also solve this error.

If not by this you can use transient keyword with your list. like this :

public transient List<Employment_Data__c> EmpDataList{get; private set;} 

Go through this document given by salesforce :

https://help.salesforce.com/apex/HTViewSolution?urlname=State-size-limit-1327107599450&language=en_US

1
  • I can not filter result because i need all records, but using transient issue is solved. Thanks Nitin
    – VML
    Jan 19, 2016 at 8:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .