I've created a Visualforce Page that enables me to upload files into our org as attachments based off Uploading a Document using Visualforce and a Custom Controller

However, what I want it to do, is to be able to upload into a specific record for a custom object I have. The custom object is called Product Donations.

How can I create a link from a Product Donation record that takes me to the new Visualforce page and enables me to upload files directly into the source record.

1 Answer 1


There are multiple ways to store (UPLOAD) files in Salesforce:

  1. Attachments
  2. Documents
  3. Chatter files
  4. Content Management

The article you are referring is about creating documents and what you want to achieve is about creating attachments,as Attachments are specific to an object record.

Salesforce Reference : https://help.salesforce.com/HTViewHelpDoc?id=notes_add.htm&language=en_US

Update attachment ParentID with your object ID in below snippet

public with sharing class FileUploadController {

  public Attachment attachment {
    get {
      if (Attachment == null)
        attachment = new Attachment();
      return attachment;

  public PageReference upload() {

    attachment.ParentId = //Pass object ID here

    try {
      insert attachment;
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading file'));
      return null;
    } finally {
      attachment.body = null; // clears the viewstate
      document = new Document();

    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'File uploaded successfully'));
    return null;

vf Page

<apex:page controller="FileUploadController">
  <apex:sectionHeader title="Visualforce Example" subtitle="File Upload Example"/>

  <apex:form enctype="multipart/form-data">
    <apex:pageMessages />
    <apex:pageBlock title="Upload a File">

      <apex:pageBlockButtons >
        <apex:commandButton action="{!upload}" value="Save"/>

      <apex:pageBlockSection showHeader="false" columns="2" id="block1">

        <apex:pageBlockSectionItem >
          <apex:outputLabel value="File Name" for="fileName"/>
          <apex:inputText value="{!attachment.name}" id="fileName"/>

        <apex:pageBlockSectionItem >
          <apex:outputLabel value="File" for="file"/>
          <apex:inputFile value="{!attachment.body}" filename="{!attachment.name}" id="file"/>

        <apex:pageBlockSectionItem >
          <apex:outputLabel value="Description" for="description"/>
          <apex:inputTextarea value="{!attachment.description}" id="description"/>



Not the answer you're looking for? Browse other questions tagged .