6

I am creating a VF page to upload files to to the Files related list of an object :

enter image description here

I found out that files object is ContentDocument object. I am figuring out how I can create this ContentDocument object. Something like:

ContentDocument  cd = new ContentDocument (title='',parentid='',body)

Seems like this in not the approach. How can I add the file select by the user . I have a inputfile tag in my VF page

2 Answers 2

14

ContentDocument object does not allow insert DML operation in Salesforce, so we can upload it through the ContentVersion object, without ContentDocumentId. After DML on ContentVersion a new version of ContentDocument will be created for us in salesforce.

The easiest way to do this is using inputFile and assign it to ContentVersion instance. Like this:

<apex:page controller="ContentController">
<apex:form>
    <apex:inputFile value="{!file}" />
    <apex:commandbutton action="{!upload}" value="Upload" />
</apex:form>
</apex:page>

Class:

public class ContentController {
    public blob file { get; set; }

    public PageReference upload() {
        ContentVersion v = new ContentVersion();
        v.versionData = file;
        v.title = 'testing upload';
        v.pathOnClient ='/somepath.txt';
        insert v;
        return new PageReference('/' + v.id);
    }
}

If you want to share the ContentVersion file then after insert DML query the ContentDocumentId from inserted ContentVersion and use ContentDocumentLink to create association between Record and ContentVersion uploaded file.

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_contentdocumentlink.htm

Use "LinkedEntityId" field to place the record id to which this file will be associated.

Note: LinkedEntityId - Can include Chatter users, groups, records (any that support Chatter feed tracking including custom objects), and Salesforce CRM Content libraries.

hope this helps and mark this as answer if does. Thanks

6
  • Thanks. How do I relate to the record for which I am attaching the file?
    – SfdcBat
    Jul 12, 2016 at 22:03
  • Do you want to upload file for records? Like notes and attachment? Jul 12, 2016 at 22:21
  • Yes exactly! Wish to do like that
    – SfdcBat
    Jul 12, 2016 at 22:24
  • Edited the original answer with more info. if you want to upload in Notes And Attachment then use Attachment object instead of ContentVersion. Jul 12, 2016 at 22:30
  • I am struggling uploading file in contentVersion using lightning component. Can any one please guide me with this. Refer to below link for details : salesforce.stackexchange.com/questions/157024/… Jan 23, 2017 at 9:02
9

I found a way to insert ContentVersion and attach it to Record in one SOQL.

<apex:page controller="ContentController">
    <apex:form>
        <apex:inputFile value="{!file}" />
        <apex:commandbutton action="{!upload}" value="Upload" />
    </apex:form>
</apex:page>

Class:

public class ContentController {
    public blob file { get; set; }

    public PageReference upload() {
        ContentVersion v = new ContentVersion();
        v.versionData = file;
        v.title = 'testing upload';
        v.pathOnClient ='/somepath.txt';
        v.FirstPublishLocationId = parentObj.Id; //similar to parentid
        insert v;
        return new PageReference('/' + v.id);
    }
}
2
  • 1
    This is much simpler, and deserves more upvotes. Oct 27, 2020 at 23:53
  • Glad, I am able to help. Thanks for your support @MatthewSouther.
    – Kritik
    Oct 28, 2020 at 5:28

You must log in to answer this question.

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