Wednesday, February 13, 2013

Transient Keyword

Many times we are struggling with view state error on visualforce page at the time of uploading files.
Salesforce provides facility to a upload file upto 10mb, But when we try to upload file through visualforce page then visualforce page limit (135kb) create a wall. It is I try to jump this wall and upload the files through visual force page.

Using Transient Keyword  we can achieve this. Decalaring Variables as transient to reduces view states. View state maintains state of the Database between request and Visualforce page.

I try to explain this using code -

Controller
public class UploadFileDemo


    public Attachment attch{get;set;} 
    public Transient blob myAttachedBody {get; set;}
    
    public UploadFileDemo()
    {
          attch = new Attachment();
    }
    public void uploadFile()
    {
          attch.body = myAttachedBody;
          attch.parentId = parentIdOfRelatedObject;
          insert attch;
    }
}
VisualForce page
<Apex:page>
  <apex:form>
      <Apex:pageblock> 
          <apex:inputFile id="idSelectFile" fileName="{!attch.Name}"  value="{!myAttachedBody}" contentType="{!attch.contentType}"  ></apex:inputFile>
            <apex:commandbutton value="Upload" action="{!uploadFile}"/>
      </apex:pageblock>
  </apex:form>
</apex:page>






1 comment:

  1. Nice information, many thanks to the author. It is incomprehensible to me now, but in general, the usefulness and significance is overwhelming. Thanks again and good luck!

    PIC Scheme Singapore

    ReplyDelete