From the Chatter tab I can go to 'Files' and then upload a file without attaching it to anything.

I am wanting to know if there is a way to achieve the exact same thing via Apex?

  • Chatter messages (private messages) can't have attachments - how would you relate the two to each other? There is no relationship available. Jul 10 '14 at 5:33
  • 1
    I realise that Chatter messages can't have attachments. From the Chatter tab I can go to Files and then upload a file without attaching it to anything. I was ultimately just wanting to know if there was a way to achieve the exact same thing via Apex? Jul 10 '14 at 20:50

If you're asking how to upload files via Visualforce, or even just via Apex, there are code samples around: https://github.com/TehNrd/Multi-File-Uploader-Force.com shows how to upload via Visualforce to the Attachments object. To upload a File in general, you want to go to the ContentVersion object.

This is some code I wrote a while ago:

public static ContentVersion generateContentVersionFile(Boolean doInsert) {
    return generateNewContentVersionVersion(null, doInsert);

public static ContentVersion generateNewContentVersionVersion(Id contentDocId, Boolean doInsert) {
    ContentVersion cont = new ContentVersion();

    if (contentDocId != null) {
        cont.ContentDocumentId = contentDocId;

    cont.Title = 'Title for this contentVersion';
    cont.PathOnClient = 'file_' + Datetime.now().getTime() + '.txt';
    cont.VersionData = Blob.valueOf('My Content in file_' + Datetime.now().getTime() + '.txt');
    cont.Origin = 'H';

    if (doInsert) {
        insert cont;

    return cont;

public static FeedItem generatePostWithRelatedDocument(Id parent, Id contentVersionId) {
    FeedItem elm = new FeedItem(Body = 'Post with related document body', ParentId = parent, RelatedRecordId = contentVersionId, Type = 'ContentPost');
    insert elm;
    return elm;
  • This looks good. So are Chatter files just content documents? Jul 10 '14 at 21:48
  • Content Versions. And FeedItem and FeedComment records relate to them. Jul 10 '14 at 21:49
  • @DavidSchach Is it possible to save files using chatter files by having custom object as the ParentId and these are not be inserted into Attachment object!!
    – Sapthagiri
    Nov 1 '14 at 12:36
  • 5
    @sapthagiri Before you ask that question, try it. Don't ask if something is possible; say if it worked when you did it and provide code if it didn't. Nov 1 '14 at 15:56

Create Content Version

String yourFiles = 'All your info goes here';

ContentVersion conVer = new ContentVersion();
conVer.ContentLocation = 'S'; // S specify this document is in SF, use E for external files
conVer.PathOnClient = 'ionicLogo.png'; // The files name, extension is very important here which will help the file in preview.
conVer.Title = 'Proposal '; // Display name of the files
conVer.VersionData = EncodingUtil.base64Decode(yourFiles); // converting your binary string to Blob
insert conVer;

Link Content Version with record using Content document

// First get the content document Id from ContentVersion
Id conDoc = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:conVer.Id].ContentDocumentId;

//Create ContentDocumentLink
ContentDocumentLink cDe = new ContentDocumentLink();
cDe.ContentDocumentId = conDoc;
cDe.LinkedEntityId = yourObjectRecord.Id; // you can use objectId,GroupId etc
cDe.ShareType = 'I'; // Inferred permission, checkout description of ContentDocumentLink object for more details
cDe.Visibility = 'InternalUsers';
insert cDe;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.