Uploading a Document into Salesforce using Visualforce

3:27 PM

In this article we will be understanding the procedure to upload a file into "Documents" using a Visualforce Page. You can upload a file and save it as an attachment against a record i believe, but i am not sure how to do this. Hence, this topic would only cover uploading a file to "Documents" under a folder you desire.

I actually had to upload a file against a record. I uploaded the file into Documents, and then stored the reference of the file as a Field in the record. This way i can access the document from the record directly at a later point.

<apex:inputfile> is the tag that is used to upload a file.

Code Sample:

Create the following Visualforce Page

<apex:page controller="UploadDoc">
  <apex:sectionHeader title="Upload a Document into Salesforce"/>
      <apex:pageblocksection columns="1">
          <apex:inputfile value="{!myimage.body}" filename="{!myimage.Name}" />
          <apex:commandbutton value="Save" action="{!Savedoc}"/>

The controller would be named "UploadDoc" and will have the following code

public class UploadDoc
Public Document mydoc;
    Public Document getmyimage()
        mydoc = new Document();
        return mydoc;
    Public Pagereference Savedoc()
        mydoc.folderid = UserInfo.getUserId(); // Stores in the currennt users "My Personal Documents" folder
        //mydoc.folderid = 'any other id you want here'; // Fetch Id dynamically by using [Select Id from Folder where Name='My Images']. Specifying a FolderId is mandatory
        insert mydoc;
        return NULL;


  1. This was exactly what I was looking for. Thanks for posting it! Please also let know if you find how to save it as an attachment to a record.

  2. Thanks for stopping by and commenting.. Sure, will post the code you asked

  3. Please tell me how you stored the reference of the file as a Field in the record. I am trying to to the same.

  4. Create a field called DocumentId in your object.. When you insert the document after upload, store this ID in the new field you created.. Later, you can navigate to the document by using the URL .. '/DocumentId'

  5. was facing an error"Variable does not exist: folderid".May i know what would be the problem?

  6. Weird!!! So you got this error when you try to save the apex class? What is the API version you use? and lastly, can you check the list of fields available for the object DOCUMENT?

  7. i wanted, how to override Opportunity button in sfdc

  8. Just exactly what I needed, thank you very much for sharing this.

  9. i copy this code and when i am going to save it then it show an error " Variable does not exist: folderID at line 13 column 10 "

  10. Ablove code si working fine for me but I am facing isssue.
    1- whn i am uploding the file thn the contentType is null for all the uploaded files.
    how to get the ContentType of the file..
    you can run the given below query.
    SELECT Name,AuthorId ,type,Keywords,Url,BodyLength,Body ,FolderId FROM Document

  11. Please tell me How to Dynamically add a watermark to an uploding images using Javascript/Jquery in Visualforce page?