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
metacube_rahulmetacube_rahul 

ISSUE: Viewstate is greater than 128k

Following is the sample code which i am using to save maximum of 10 attachments at a time.

But whenever the size of the attachments gets larger the page displays the view state error message

For smaller size attachments the code works perfectly but whenever the size of any attachment increases

, the salesforce internal limit error occurs for viewstate.(128K) 

 

VF Page 

-------------------------------------------------------------

<apex:page controller="ReAttachmentController">

<apex:form >
<apex:pageMessages />
 <apex:repeat value="{!aList}" var="a">
   <apex:inputFile style="width:500px" value="{!a.body}" filesize="{!a.BodyLength}" filename="{!a.name}"/>
 </apex:repeat>
 

 <apex:commandButton value="Attach" action="{!doAttach}"/>
</apex:form>
</apex:page>

 

 

 

APEX CODE

--------------------------------

 

public class ReAttachmentController {
  public List<Attachment> aList{get;set;} 
  private String caseId = '50040000007GIsV'; 
  public ReAttachmentController(){
       aList = new List<Attachment>();
       for(Integer attachmentNos = 0 ; attachmentNos < 10; attachmentNos++){
             Attachment attach = new Attachment();
             aList.add(attach);
       }
  } 
  public boolean validateAttachments(){
        boolean returnValue = true;
        for(Attachment ab : aList){
              if(ab.body == null){
                   returnValue = false;
              }
        }       
        return returnValue;
  }
   
  public PageReference doAttach(){
      List<Attachment> lstToAttach = new List<Attachment>();
      boolean returnValue = validateAttachments();
         if(returnValue){
               for(Attachment ab : aList){
                    ab.parentId = caseId;
                    lstToAttach.add(ab);
               }
               insert lstToAttach;
               for(Attachment ab : aList){
                    ab.body = null;
               }
              
               return null;
         }else{
              return null;
         }             
  }
}

PamSalesforcePamSalesforce

Hi,

 

Were you able to resolve this issue?

I am running into the same problem, do let me know iof you found a solution.