4

How can I avoid this error? I read similar post without getting any working solutions for me. When I try to upload file larger than about 150k, I get the error.

 <apex:page controller="FormCurriculumController" showHeader="false">

<script>

 function show() {
    document.getElementById("myDiv").style.display="block";

}

</script>



<style type="text/css">
body { background-color:#FFF; }
.erroreMsg, .required { color:#F00; }
</style>

<apex:form styleClass="wwwtesto" rendered="{!NOT(saved)}">
    <p>Fields marked with an * are mandatory.</p>
    <apex:messages styleClass="required" />
    <br/>
    <p>
        <apex:outputLabel value="First name *" for="firstName"/>
        <apex:inputField id="firstName" required="true" value="{!curriculum.FirstName__c}"/>
    </p>
    <p>
        <apex:outputLabel value="Last name *" for="lastName"/>
        <apex:inputField id="lastName" required="true" value="{!curriculum.LastName__c}"/>
    </p>
    <p>
        <apex:outputLabel value="Email *" for="email"/>
        <apex:inputField id="email" required="true" value="{!curriculum.Email__c}"/>
    </p>
    <p>
        <apex:outputLabel value="Message" for="note"/>
        <apex:inputField id="note" required="false" value="{!curriculum.Note__c}"/>
    </p>
    <p>
        <apex:outputLabel value="Attachment" for="resume"/>
        <apex:inputFile id="resume" fileName="{!fileName}" contentType="{!contentType}" value="{!resume}" size="45"/>
    </p>
    <p>
    <apex:inputCheckbox id="privacy" required="true" value="{!privacy}"/>I authorise the use of my personal data in compliance with Legislative Decree 196/03.
    </p>
    <p><apex:commandButton value="Send" action="{!save}" onclick="show()"/></p> 
    <div id="myDiv" style="display:none;"><apex:image value="{!$Resource.Loader}"/></div>
  </apex:form>
  <apex:outputText styleClass="wwwtesto" rendered="{!saved}" value="Thank you for submitting your resume."/>

</apex:page>

CLASS:

public class FormCurriculumController { 
public Curriculum__c curriculum {get; set;}
public Boolean privacy {get; set;}
public Boolean saved {get; set;}
public String styleClass {get; set;}
// Allegato
public Blob resume {get; set;}
public String contentType {get; set;}
public String fileName {get; set;}


public FormCurriculumController () {
    curriculum = new Curriculum__c();
    saved = false;
}

public PageReference save() {
    try {
        // Validazione effettuata qui perchè il required sull'inputCheckbox non è utilizzabile
        if (!privacy) {
            ApexPages.Message reqMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Autorizzare il trattamento dei dati personali');
            ApexPages.addMessage(reqMsg);
            return null;
        }

        // Inserisce il record
        insert(curriculum);
        // Inserisce l'allegato
        if (resume != null) {
            Attachment attach = new Attachment();
            attach.Body = resume;
            attach.Name = fileName;
            attach.ContentType= contentType;
            attach.ParentId = curriculum.id;                
            try {
                insert(attach);

            } catch (Exception ex) {
                // TODO fare il roolback dell'inserimento del curriculum?

                ApexPages.addMessages(ex);
                return null;
            }                    
        }

        //Papagni 03/09/2013 - update curriculum per fare scattare il trigger di after che invia la mail
        update curriculum;

        saved = true;

    } 
    catch(Exception ex) {
        ApexPages.addMessages(ex);  
    }
    return null;   

}

}

  • 4
    Did you try making resume transient to keep it out of the view state? i.e. public Transient Blob resume {get; set;} – BarCotter Oct 13 '14 at 10:40
  • fine, it works! – user9959 Oct 13 '14 at 10:42
14

The View State will most likely be caused by this tag because resume could be a rather large Blob.

    <apex:inputFile id="resume" fileName="{!fileName}" contentType="{!contentType}" value="{!resume}" size="45"/>

You don't need to store resume in the View State. You can use the Transient Keyword to make sure its not included in the View State. If you change your declaration of resume to what is shown below then it should stop your issue:

public Transient Blob resume {get; set;}

From sfdcfox's answer:

Transient variables are not stored in the view state, but can be regenerated by the post back if bound to an input element. In general, you only need to use the view state for elements not bound to an input element and need to maintain state between post backs.

1

It is enough to set resume = null;, empty fields are not going in viewstate.

  • 1
    @martin well, I had the same issue as asker, and this solution worked for me. So this is possible way of avoiding asker's issue. – Vladyslav K Dec 13 '15 at 11:03
  • My apologies, I misread your answer. – martin Dec 14 '15 at 1:30

Your Answer

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