-2

i have a VF page in which users can upload files. The issue i'm encountering is if the file is a certain size (Which is a relatively small size), the user is shown a screen that says "

Maximum view state size limit (170KB) exceeded. Actual view state size for this page was 1,006.033KB (or whatever size the given file was".

The funny thing is that the files are successfully uploading, so the only issue i need to solve here is to stop showing that message. HOw can I do so? VF page and controller here:

VF Page:

<apex:page controller="ContractsFormPageController" showHeader="false" applyHtmlTag="true" applyBodyTag="false"> <head> <apex:slds /> </head>
<style>
       @page {
    display: block;
    white-space: nowrap;
     }
    h1{
    font-size:18px;
    color: #4A318E;
    font-size:35px;
    align:center;
    text-align:center;
    font-family:calibri;
    white-space: nowrap;
    }
    .logo{
    padding-left:25%;
    font-size:34;
    font-weight:bold;
   color: #4A318E;
    white-space: nowrap;
    }
    .title{
    padding-left:25%;
    font-size:40px;
    font-weight:bold;
   color: #4A318E;
    white-space: nowrap;
    }
    .sectionsIndent{
    margin-left: 25%;
    margin-right: 25%;
    white-space: nowrap;
    }
    .inputFields{
    font-size:16px;
    margin-left: 12%;
    font-family:calibri;
     display: block;
    white-space: nowrap;
    }
    
    .header {
      text-align: left;
      font-weight:bold;
      color: black;
      font-family: 'Arial';  
      margin: 0;
      background-color:  rgb(243, 242, 242);
      font-size:16px;
    white-space: nowrap;
        }
    h3{
    text-align:center;
    font-weight:bold;
    color:black;
    font-family:calibri;
    font-size:18px;
    background-color:  rgb(243, 242, 242);
     display: block;
    white-space: nowrap;
}

    </style> 
    <br/>

   <div class="logo">
        <img src="https://dy5f5j6i37p1a.cloudfront.net/company/logos/155349/300x145/d1bce8c4172e11eb84287ef9e0e302ef.png" width="180" height="80" alt="CHC Logo" align="left"/> 
       <br/><br/>
       <h1>
       <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Contract Request Portal</b>
       </h1> 
    </div>
    <br/><br/><br/>

    
<apex:form styleclass="sectionsIndent">
    <apex:outputPanel rendered="{!displayMainPage}">
        <h3>
            Contract Request Details
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          
        </h3><br/><br/>
    <div class = "inputFields">
   <!--    (Vendor Lookup:&nbsp; <apex:inputField value="{!accName.Vendor_name__c}"/>)<br/><br/> -->
       Requester name:&nbsp; <apex:inputText style="border:0px solid #58B9FA; border-bottom: 1px solid black;" value="{!requesterName}" /> <br/><br/>
       Requester email:&nbsp; <apex:inputText style="border:0px solid #58B9FA; border-bottom: 1px solid black;" value="{!requesterEmail}" /> <br/><br/>   
       Requester phone:&nbsp; <apex:inputText style="border:0px solid #58B9FA; border-bottom: 1px solid black;" value="{!requesterPhone}" /> <br/><br/>   
       Vendor name:&nbsp;<apex:inputText style="border:0px solid #58B9FA; border-bottom: 1px solid black;" value="{!vendorName}" /> <br/><br/>   
       Vendor email:&nbsp; <apex:inputText style="border:0px solid #58B9FA; border-bottom: 1px solid black;" value="{!vendorEmail}" /> <br/><br/>   
       Vendor phone:&nbsp;<apex:inputText style="border:0px solid #58B9FA; border-bottom: 1px solid black;" value="{!vendorPhone}" /> <br/><br/>   
       Facility:&nbsp;     <apex:selectList style="align:center;text-align:center" value="{!facility}" multiselect="false" size="1">
                            <apex:selectOption itemValue="" itemLabel="--- Please Select ---"/>
                            <apex:selectOption itemValue="abc" itemLabel="abc"/>
                            <apex:selectOption itemValue="defg" itemLabel="defg"/>

                        </apex:selectList> <br/><br/>  
     <!--  Facility:&nbsp; <apex:inputText style="border:0px solid #58B9FA; border-bottom: 1px solid black;" value="{!facility}" /> <br/><br/>   -->
       Contract type:&nbsp; <apex:selectList style="align:center;text-align:center" value="{!contractType}" multiselect="false" size="1">
                                <apex:selectOption itemValue="" itemLabel="--- Please Select ---"/>
                                 <apex:selectOption itemValue="abc" itemLabel="abc"/>
                                 <apex:selectOption itemValue="def" itemLabel="def"/>
     
                            </apex:selectList> <br/><br/>  
      Upload File:&nbsp;  <apex:inputFile id="if1" value="{!file1}" fileName="{!fname}" onchange="check(this)"  />
    </div>
    <br/>
        
    <div align="center"  draggable="false" >
       <apex:commandbutton style="width:150px;font-weight: bold;height: 40px;font-size:16px; font-family:calibri;"  action="{!saveTextValue}" value="Submit"></apex:commandbutton>
    </div>
    </apex:outputPanel>
    
            <apex:outputPanel layout="block" rendered="{!displaySubmittedPage}"> 
                <div style = "font-family:calibri; font-size:20px;text-align:center">
                 <br/><br/><br/><br/><br/>
                 <b> Request Submitted!</b>
                <br/>
                </div>
            </apex:outputPanel>
    
    
            <apex:outputPanel layout="block" rendered="{!problemuploadingfile}"> 
                <div style = "font-family:calibri; font-size:20px;text-align:center">
                 <br/><br/><br/><br/><br/>
                    <b>There was a problem uploading the file. Please submit request without adding a file.</b>
                <br/>
                 <div align="center"  draggable="false" >
                <apex:commandButton value="Return" action="{!showpopup}"/>
                </div>
                <br/>
                </div>
            </apex:outputPanel>

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

CONTROLLER:

public class ContractsFormPageController {
    
    public string myTestString {get;set;}
    
    public string requesterName {get;set;}
    public string requesterEmail {get;set;}
    public string requesterPhone {get;set;}
    public string vendorName  {get;set;}
    public string vendorEmail {get;set;}
    public string vendorPhone {get;set;}
    public string facility {get;set;} 
    public string contractType  {get;set;} 
    public boolean displayMainPage {get;set;}
    public boolean displaySubmittedPage {get;set;}
    public account acct {get;set;} 
    public blob file1{get;set;}
    public String fname{get;set;}  
    public boolean problemuploadingfile {get;set;}
    
    
    
    public ContractsFormPageController(){
    myTestString = 'Variable Test Unit';
        displayMainPage = true;
    }
    
    public void showpopup(){
        displaymainpage = true;
    }
    
    public vendor_contract__c getAccName(){   
         return [select Vendor_name__c from vendor_contract__c  order by name desc
                 limit 1];
        }
    
    public void saveTextValue(){
        RecordType rt =[select id,Name from RecordType where sObjectType='Account' AND Name='Vendor' limit 1];
        Account  a = new Account();
        ContentVersion cv = new ContentVersion();
        
        //if(vendorName != null){
      //  a.PersonEmail = vendorEmail;
        if (vendorPhone != null){
            a.phone = vendorPhone;
        }
            a.name =  vendorName; 
           a.recordtypeid = rt.id;
            insert  a; 
      //     }
        
        
        Vendor_Contract__c contract = new Vendor_Contract__c();
        if (requesterName !=null){
             contract.requester_name__c = requesterName;
        }
        if (requesterEmail != null){
             contract.requester_email__c = requesterEmail;
        }
        if (requesterPhone != null){
             contract.requester_phone__c = requesterPhone;
        }
      
                   contract.name = vendorName;
                   contract.vendor_name__c = a.id;
        if (vendorEmail != null){
            contract.vendor_email__c = vendorEmail;
        }
        if (vendorPhone != null){
             contract.vendor_phone__c = vendorPhone;
        }
        if (contractType != null){
            contract.contract_type__c = contractType; }
        if (facility != null){
            contract.facility__c = facility; }
        
        date d = date.today();
         string todaysDate = string.valueof(d);
          string year = todaysDate.substring(0,4);
          string month = todaysDate.substring(5,7);
          string day = todaysDate.substring(8,10);
          todaysDate = month + '/' + day + '/' + year;
        
        contract.contract_full_name__c = vendorName + '-' + facility + '-' + todaysDate;
        
        insert contract;
        
       
        if (fname != null){
            try{ 
      if(Test.isRunningTest()) {
            blob b;
             b = blob.valueOf('Unit.Test');
            cv.versionData = b;
        }
        else{
        cv.versionData = file1;
        }
            
        cv.title = fname;
        cv.pathOnClient ='/somepath.pdf';
        cv.attach_to_auth_request__c = true;
        insert cv;

       ContentVersion Content = [SELECT id, ContentDocumentId FROM ContentVersion where Id = :cv.Id];
        ContentDocumentLink contentlink=new ContentDocumentLink();
        contentlink.LinkedEntityId=contract.id;
        contentlink.ShareType= 'V';
        contentlink.ContentDocumentId= Content.ContentDocumentId;
        insert contentlink;
            }catch (exception e){
                problemuploadingfile = true;
            }
        }
        
        
        displayMainPage = false;
        displaySubmittedPage = true;
}
    
    
}
1

You're basically trying to store the entire file in the view state, which causes this error. You need to omit the file contents from the view state using the transient keyword:

public transient blob file1{get;set;}
3
  • I am not sure if the VF page will accept values of transient variable from client. Personally I always solve similar problems with VF remoting or JSForce lib. Last one does allow to upload bigger attachments – kurunve Apr 13 at 21:02
  • 1
    @kurunve I've used this successfully many times. I do agree though, that the maximum file size would be 6MB, so if the user needs something that supports larger files, they need to go with a client-side solution. – sfdcfox Apr 13 at 21:16
  • @sfdcfox thank you that did the job! Just wondering, what would a client-side solution look like? – Michael M Apr 15 at 14:01

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.