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
Robert ZentgrafRobert Zentgraf 

Upload documents by using button on a custom objects and store it in document folder

Hi,
can please someone help me. I created a visualforce page to upload a document on my custom object. But I have issues with the apex-class. How can be the solution?

VF:​
  1. <apex:page StandardController="Profil__c" extensions="documentExt">
  2. <apex:messages />
  3. <apex:form id="theForm">
  4. <apex:pageBlock >
  5. <apex:pageBlockSection >
  6. <apex:inputFile value="{!document.body}" filename="{!document.name}"/>
  7. <apex:commandButton value="Save" action="{!save}"/>
  8. </apex:pageBlockSection>
  9. </apex:pageBlock>
  10. </apex:form>
  11. </apex:page>
Apex:
  1. public with sharing class documentExt {
  2.     public Document document{
  3.         get{
  4.             if (document == null)
  5.             document = new Document();
  6.             return document;
  7.         }
  8.         set;
  9.     }
  10.     public documentExt(ApexPages.StandardController controller) {
  11.         Document d = (Document) controller.getRecord();
  12.         d.folderid = '00lc0000000DyIj'; //special folder
  13.     }                 
  14. }
Regards
Robert
Suneel#8Suneel#8
I think you are missing to insert the Document you created
VF Page code
<apex:page controller="FileToDocument">
    <apex:form >
    <apex:inputFile fileName="{!d.name}" fileSize="{!d.bodyLength}" value="{!d.body}"></apex:inputFile>
    <apex:commandButton action="{!upload}" value="Attach"/>
    </apex:form>
</apex:page>
Controller Code
public class FileToDocument {

    public PageReference upload() {
        d.folderId='00590000000yBOm';
        insert d;
        return null;
    }

    public Document d {get;set;}
    Public FileToDocument(){
        d=new Document();
    }
}