Upload a file as an Attachment to a record using Visualforce

9 comments
This example illustrates the way to upload a file and attach it against any record using Visualforce. The file that you upload using this will be available in the "Notes & Attachments" related list for the record.

Click here if you are looking to upload a file into "Documents".

Step 1: Create an Apex class named "attachmentsample" . Paste the code below.


public class attachmentsample {

    public attachmentsample(ApexPages.StandardController controller) {

    }
    Public Attachment myfile;
    Public Attachment getmyfile()
    {
        myfile = new Attachment();
        return myfile;
    }
   
    Public Pagereference Savedoc()
    {
        String accid = System.currentPagereference().getParameters().get('id');

        Attachment a = new Attachment(parentId = accid, name=myfile.name, body = myfile.body);
         
         /* insert the attachment */
         insert a;
        return NULL;
    }   

}

Step 2:

Create a Visualforce page named "attachment" and paste the code below.


<apex:page standardController="Account" extensions="attachmentsample">

<apex:form >
  <apex:sectionHeader title="Upload a Attachment into Salesforce"/>
  <apex:pageblock >
      <apex:pageblocksection columns="1">
          <apex:inputfile value="{!myfile.body}" filename="{!myfile.Name}" />
          <apex:commandbutton value="Save" action="{!Savedoc}"/>
      </apex:pageblocksection>
  </apex:pageblock>   
</apex:form> 

</apex:page>

Quick Test:
If you do not wish to do Step3 and 4 do this. Paste the following URL in your browser
http://yoursalesforceinstance.com/apex/attachment?id=0017000000eiDgy

ID should be a Valid Account Id of your instance.

Step 3:
Create a custom button (Detail Page button) on Account and select the new Visualforce page that you created as the source.

Step4:
Add the button created in Step3 to the Account Page layout.

You can test this by navigating to any account and clicking on the button that you created in Step3.
You may test

9 comments :

  1. Awesome... Thanks

    ReplyDelete
  2. nice work, now if only you could upload it to Content and select a public workspace to publish it to.

    ReplyDelete
  3. How do you write the test case for the above controller?

    ReplyDelete
  4. How can i display list of attached documents?

    Regards,
    Devendra S

    ReplyDelete
  5. error while uploading:
    System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Parent]: [Parent]

    ReplyDelete
  6. Nice one :) its working fine, have any idea how to download the attachments using Bulk API ?

    ReplyDelete
  7. What if I want to just add an attachemnt to an VF page I created?

    ReplyDelete
  8. Clarification, I want to add attachments to the VF page yet when saved, available as related lists within SF

    ReplyDelete
  9. This is a fantastic work around for the permission limitations of portal users not being able to attach documents to records.

    ReplyDelete