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
JasonGablerJasonGabler 

Exceeding state limit size, not sure why

The Visualforce and Apex below is being used via an externally, anonymously accessible Force.com site.  The error text is:

 

  • Maximum view state size limit (128KB) exceeded. Actual view state size for this page was 149.953KB

 

 

 

I've been reading more than a few threads on file upload sizes and the state size, but most are old.  In fact, most claim you can do a 5MB upload, so I don't understand the problem, because, despite what I describe next, this error only shows up when I upload a file that's more than 100K or so.

 

Now, I do have a rather large object floating around in the state (on the order of one hundred fields, with many text areas and rich text areas of up to 4500 char limit).  This object is kept around because I am in a 10 page wizard and figure loading it all the time might not be as good just keeping it readily available, especially since I cannot load all fields without doing the ridiculous task of having to inspect the schema to get all field names etc...

 

Anyhow,  I'm going to guess this isn't a one shot answer.  So I'm expecting some more questions and discussion.

 

jason

 

 

 

 

 

<apex:inputFile id="irs_2009" value="{!uploadBody}" filename="{!rfq.irs_2009__c}" styleClass="input-file"/>
<apex:commandButton value="Upload Attachment" action="{!irs2009Upload}"  styleClass="upload-button"/>

public Blob uploadBody {get; set;}   
public PageReference irs2009Upload() { uploadFile('IRS 990 2009 - '+rfq.irs_2009__c); return null;}

public void uploadFile(String uploadName) {
    if(uploadBody != null && uploadName != null) {  
      Attachment att = new Attachment();  
      att.Body = uploadBody;
      att.Name = uploadName;
      save(); // this ensures that 'rfq' in the next line is not null
      att.ParentId = rfq.Id;  
      insert att;    	
    }   
  }

 

 

 

 

 

 

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
JasonGablerJasonGabler

Wonderful link, thanks so much Stephen.

 

And this solved the problem at hand:

 

transient public Blob uploadBody {get; set;}   

 

The issue being, as I am sure you know, the Blob remained in-state causing the exception to be thrown.  Once I put the Blob in the Attachment and insert it there is no reason for it to remain in state.  It wasn't the upload that was causing the problem but that the contents of the upload remained in memory as Salesforce was attempting to save state and post back to my browser.

 

I'll be using transient regularly :)  Thanks again,

 

jason

 

 


All Answers

stephanstephan

A good article discussing view state can be found here:

 

http://wiki.developerforce.com/index.php/An_Introduction_to_Visualforce_View_State

 

But in terms of your approach, I suspect that for large objects with lots of content in rich text fields you're going to be better off requerying than relying on view state.

 

...stephan

JasonGablerJasonGabler

Wonderful link, thanks so much Stephen.

 

And this solved the problem at hand:

 

transient public Blob uploadBody {get; set;}   

 

The issue being, as I am sure you know, the Blob remained in-state causing the exception to be thrown.  Once I put the Blob in the Attachment and insert it there is no reason for it to remain in state.  It wasn't the upload that was causing the problem but that the contents of the upload remained in memory as Salesforce was attempting to save state and post back to my browser.

 

I'll be using transient regularly :)  Thanks again,

 

jason

 

 


This was selected as the best answer
jwetzlerjwetzler

Ah glad you solved it, was just coming in here to tell you to mark that field as transient.

 

There's one additional gotcha that might be important to note, which is that if you were to move that Attachment declaration out to the class level instead of creating it inside of the method, you'd either need to make your Attachment transient, or you'd need to null out the body field after you do the upload, to prevent the data in the body field from taking up all of your viewstate.  A couple of threads on this already:

http://community.salesforce.com/t5/Visualforce-Development/Error-Maximum-view-state-size-limit-128K-exceeded-uh-oh/td-p/87434

Most notably the post that's marked as a solution and the question immediately above it.

And then this one:

http://community.salesforce.com/t5/Visualforce-Development/Viewstate-size-limit-error/m-p/153039

 

But yeah it looks like you've got what you need.