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

uploaded files to be saved in personal folder



Can anyone help me by answerwing, how to save the uploaded file in visualforce page to some "TEST" folder in documents. Please check teh code below:


VF page:

<apex:page standardController="Account" extensions="AttachmentUploadExtension">
<apex:form enctype="multipart/form-data">

  <apex:pageMessages />
<apex:pageBlock >
<apex:pageBlockSection title="Attachment for Survey Monkey File" columns="2"/>
<apex:pageBlockSection >
        <apex:pageBlockSectionItem >
          <apex:outputLabel value="File Name" for="fileName"/>
          <apex:inputFile value="{!document.body}" filename="{!}" id="fileName"/>

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






public class AttachmentUploadExtension{

  public AttachmentUploadExtension() {}
  public ID folderid{get;set;}

  public Attachment attachment {
  get {
      if (attachment == null)
        attachment = new Attachment();
      return attachment;
  public AttachmentUploadExtension(ApexPages.StandardController controller) {

  public PageReference upload() {
    attachment.OwnerId = UserInfo.getUserId();
              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;






i am not seeing any document object usage in the code......i think you need to add the below lines



   in your upload method you need to add the below points


get the respective folder id by using a query ....and you need to create a document object record to store in the respective folder..


folder obj = [select id from folder where name = 'x'];


document obj = new document();

obj.folderid = UserInfo.getUserId(); //this puts it in My Personal Documents some thing like this ....

insert obj;