function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
swapnil kaleswapnil kale 

View State error while creating generating an .CSV file

Error that I am getting is "Maximum view state size limit (135KB) exceeded. Actual view state size for this page was 140.375KB"

I am having a page which has an link i.e, Page 1
<apex:page controller="OpportunitiesRollOverWizardController"
    sidebar="false" >
                        <apex:commandLink value="Export details in .csv format"
                            action="{!downloadOpportunities}" />  
</apex:page>
Page 2 is used to create an .CSV file
<apex:page controller="OpportunitiesRollOverWizardController" cache="true" contentType="text/csv#Export.csv" language="en-US" >"Name","Owner Name","Owner Id","Account Name","Account Id","Stage Name","Probability","Product Family","Total Opportunity Quantity","Amount","Description","Close Date"
<apex:repeat value="{!listOpportunitiesToDownload}" var="record">
<apex:repeat value="{!record}" var="subRecord">
"{!subRecord.Name}","{!subRecord.Owner.FirstName} {!subRecord.Owner.LastName}","{!subRecord.OwnerId}","{!subRecord.Account.Name}","{!subRecord.AccountId}","{!subRecord.StageName}","{!subRecord.Probability}","{!subRecord.Product_Family__c}","{!subRecord.TotalOpportunityQuantity}","{!subRecord.Amount}","{!subRecord.Description}",<apex:outputText value="{0,date,MM/dd/yy}"><apex:param value="{!subRecord.CloseDate}"/></apex:outputText>
</apex:repeat>
</apex:repeat>
</apex:page>
and a controller
 
public with sharing class OpportunitiesRollOverWizardController {
     public  List<List<Opportunity>> listOpportunitiesToDownload{ get; set; }  //List Of Opportunities to a .csv file
     List<Opportunity> listCurrentShowOpportunities;
      
     public OpportunitiesRollOverWizardController(){
        listCurrentShowOpportunities = [select Id from opportunity];
     }
     
     public PageReference downloadOpportunities() {
        PageReference oPageRef = Page.CreateCSVFileForOpprtunityRollOverWizard;
        oPageRef.setRedirect(false);
        List < Opportunity > listOpportunityForCSVFile = new List < Opportunity > ();

        //At the max 1000 Oppties are added to the list for downloading. This is to prevent salesforce governor limits from exceeding.
        //So we are maintaining list of list of Oppties. Each list of 1000 records. These Oppties will then be appended to the final download file.
        for (Integer i = 0; i < listCurrentShowOpportunities.size(); i++) {
            if (listOpportunityForCSVFile.size() < 1000) {
                listOpportunityForCSVFile.add(listCurrentShowOpportunities[i]);
            } //End of if
            else {
                listOpportunitiesToDownload.add(listOpportunityForCSVFile);
                listOpportunityForCSVFile = new List < Opportunity > ();
                listOpportunityForCSVFile.add(listCurrentShowOpportunities[i]);
            } //End of else
        } //End of for
        listOpportunitiesToDownload.add(listOpportunityForCSVFile);
        return oPageRef;
    } //End of downloadOpportunities()
}
      
When I am having more number of opportunity i am getting view state error. Doing listOpportunitiesToDownload Transient also have not solved my problem getting Attempt to de-reference a null object error.I have also refrenced to https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_System_PageReference_setRedirect.htm and setRedirect(Boolean) to false. But I am getting error for view state. Please need help in solving the view state error.
Thanks, Best Regards.
Arunkumar RArunkumar R
Hi Swapnil,

I just updated code, please try the below one. As we you need to export only 1000 records right, hence i put Limit on SOQL query.

1. You are going to display many fields, So query all field name that you want in your query. For demo, i used only the name.
2. You not initialized the list in downloadOpportunities method, so that one i made changes.

Here is the full code,
 
public with sharing class OpportunitiesRollOverWizardController {
     public  List<List<Opportunity>> listOpportunitiesToDownload{ get; set; }  //List Of Opportunities to a .csv file
     List<Opportunity> listCurrentShowOpportunities;
      
     public OpportunitiesRollOverWizardController(){
        listCurrentShowOpportunities = [select Id, Name from opportunity Limit 1000];
     }
     
     public PageReference downloadOpportunities() {
        
        listOpportunitiesToDownload = new List<List<Opportunity>>();
        PageReference oPageRef = Page.CreateCSVFileForOpprtunityRollOverWizard;
        oPageRef.setRedirect(false);
        List < Opportunity > listOpportunityForCSVFile = new List < Opportunity > ();

        //At the max 1000 Oppties are added to the list for downloading. This is to prevent salesforce governor limits from exceeding.
        //So we are maintaining list of list of Oppties. Each list of 1000 records. These Oppties will then be appended to the final download file.
        for (Integer i = 0; i < listCurrentShowOpportunities.size(); i++) {
            if (listOpportunityForCSVFile.size() < 1000) {
                listOpportunityForCSVFile.add(listCurrentShowOpportunities[i]);
            } //End of if
            else {
                listOpportunitiesToDownload.add(listOpportunityForCSVFile);
                listOpportunityForCSVFile = new List < Opportunity > ();
                listOpportunityForCSVFile.add(listCurrentShowOpportunities[i]);
            } //End of else
        } //End of for
        listOpportunitiesToDownload.add(listOpportunityForCSVFile);
        return oPageRef;
    } //End of downloadOpportunities()
}

<apex:page controller="OpportunitiesRollOverWizardController"
    sidebar="false" >
    <apex:form>
                        <apex:commandLink value="Export details in .csv format"
                            action="{!downloadOpportunities}" />  
                            </apex:form>
</apex:page>

<apex:page controller="OpportunitiesRollOverWizardController" cache="true" contentType="text/csv#Export.csv" language="en-US" >"Name"
<apex:repeat value="{!listOpportunitiesToDownload}" var="record">
<apex:repeat value="{!record}" var="subRecord">
"{!subRecord.Name}"
</apex:repeat>
</apex:repeat>
</apex:page>

 
swapnil kaleswapnil kale

Thanks for replay Arunkumar

But giving limit to query is not the right solution. I may also get oppotunities greater then 1000. And yes I should query for all fields required but
problem is for view state. When I get oppty with about 1500 or greater then that it gives me view state error. How should I be solving view state error?
swapnil kaleswapnil kale
Please need help to solve this error.