I am trying to implement a page that I will upload files through it, but I keep having this error

Maximum view state size limit (135KB) exceeded. Actual view state size for this page was 158.328KB

Here is my visualforce form

<apex:pageBlockSection columns="3">
    <apex:pageBlockSection >
      <apex:pageBlockSectionItem >
         <apex:inputFile value="{!document.body}" filename="{!document.name}" id="file"/> 
     <apex:pageBlockSectionItem >
        <apex:outputLabel value="Description" for="description"/>
        <apex:inputTextarea value="{!document.description}" id="description"/>
 <apex:pageBlockButtons >
    <apex:commandButton action="{!upload}" value="Upload"/>

and here is my APEX code

public List<Document> documents{set;get;}
public Document document 
    if (document == null)
      document = new Document();
      return document;
document.AuthorId = UserInfo.getUserId();
    document.FolderId = account.ID;
    try {
       insert document;
    } catch (DMLException e) {
       ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error'));
    } finally {
       document.body = null;
       document = new Document();
       documents = [SELECT Query];

What is the problem here ?

Update: The query was like this

documents = [SELECT Description, body, Id, Name, Url FROM Document WHERE
AuthorId =:UserInfo.getUserId()];

but it works after I removed body

documents = [SELECT Description, Id, Name, Url FROM Document WHERE
AuthorId =:UserInfo.getUserId()];
  • Can you paste your update code. – Sathya Feb 20 '14 at 12:54
  • @Sathya I've just updated it. – hasan.alkhatib Feb 20 '14 at 13:00
  • I have to declare a document variable as a transient...?? – Sathya Feb 21 '14 at 14:00

Try making your Document object transient. Its not evident from your code. And what is that 'documents' in the end, where you try to query something?

  • Yes actually, but the error has changed to this "Maximum view state size limit (135KB) exceeded. Actual view state size for this page was 158.328KB" – hasan.alkhatib Feb 20 '14 at 10:02
  • 1
    Dont change the question after someone answers. That makes the answer completely out of context. Open a new question if required, after closing the previous one. Anyway, I have updated my answer. – AslamK Feb 20 '14 at 10:10
  • documents is a List<document> – hasan.alkhatib Feb 20 '14 at 11:10
  • I think that might be the problem. Dont query the document body in that query (the query in the question is not complete). – AslamK Feb 20 '14 at 11:53
  • you are right MR :D, thx. – hasan.alkhatib Feb 20 '14 at 12:19

Following information can be provided according to the question:-

  • Make document variable transient and also make document = null;
  • Also do not query body field in SOQL if you are querying that. e.eg; [SELECT id, body FROM Document]. This may hit controller heap size and page viewstate (if referenced on Visualforce page)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.