function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Code+1Code+1 

Attachment Upload through Visualforce Page

Hello All,
 I have a requirement to upload an attachment through Visual Force Page.
 Attachment should reside in Custom Object.
 
 On creation of record, user will fill some details and upload attachment from local machine.
 Details and Attachment should be captured as record in custom object.
 Please let me know for any sample code.

Thanks !
Best Answer chosen by Code+1
Abhishek BansalAbhishek Bansal
Hi please update that method as follows :

public AttachmentUploadController(ApexPages.StandardController controller) {
        Associate__c assoc = (Associate__c)controller.getRecord();
        recordId = assoc.id;
    }


Let me know if you have any issue.

Abhishek

All Answers

Abhishek BansalAbhishek Bansal
Hi Krishna,

You can find complete help regarding this on link given below :

http://blog.jeffdouglas.com/2010/04/28/uploading-an-attachment-using-visualforce-and-a-custom-controller/

Regards,
Abhishek.
Code+1Code+1
Hi Abhishek,

 Thanks for your response.
 Code works fine if the Parent Id is hardcoded.
 Here, I need to capture the attachment.ParentId dynamically on click of the custom button at Objects Detail Page.
 Please let me know, how to achieve it.

Thanks !
Abhishek BansalAbhishek Bansal
Hi,

Please use below code and modify your controller code accordingly :
public Id recordId; 

public PsaCostProjectionExtension(ApexPages.StandardController stdController) {
        
        this.recordId = (YourCustomObject__c)stdController.getRecord().Id;//Replace your Object API name with YourCustomObject__c
}
If you are not able to do the chages than please share your class code so that i can help you.

Regards,
Abhishek.
 
Code+1Code+1
Hi Abhisek,

 Please find the code below --

 VF Page
<apex:page standardController="Associate__c" extensions="AttachmentUploadController">  
  <apex:sectionHeader title="Visualforce Example" subtitle="Attachment Upload Example"/>

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

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

      <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:pageBlockSectionItem >
          <apex:outputLabel value="File" for="file"/>
          <apex:inputFile value="{!attachment.body}" filename="{!attachment.name}" id="file"/>
        </apex:pageBlockSectionItem>

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

      </apex:pageBlockSection>

    </apex:pageBlock>
  </apex:form>
</apex:page>

-------------------------------------------------------------------------
Apex Class

public with sharing class AttachmentUploadController {
    public Id recordId;

    public AttachmentUploadController() {
        
    }


    public AttachmentUploadController(ApexPages.StandardController controller) {
        this.recordId = (Associate__c)StandardController.getRecord().Id;
    }


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

  public PageReference upload() {

    attachment.OwnerId = UserInfo.getUserId();
    attachment.ParentId = recordId; // the record the file is attached to
 //   attachment.ParentId = ApexPages.currentPage().getParameters().get('id'); 
    attachment.IsPrivate = true;

    try {
      insert attachment;
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
      return null;
    } finally {
      attachment = new Attachment(); 
    }

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

}

Thanks !!
Abhishek BansalAbhishek Bansal
Are you facing any issue ?
 
Code+1Code+1
Hi Abhishek,

 Yes, this.recordId = (Associate__c)StandardController.getRecord().Id;
 is not recognized by Salesforce
Abhishek BansalAbhishek Bansal
Hi please update that method as follows :

public AttachmentUploadController(ApexPages.StandardController controller) {
        Associate__c assoc = (Associate__c)controller.getRecord();
        recordId = assoc.id;
    }


Let me know if you have any issue.

Abhishek
This was selected as the best answer
Amit SAmit S
Hi Krishna, What was the correct code you used to solve this? I am facing same issue with my loading of attachments on a VF Page while the object is not yet saved and not having the Id.