14

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?

2
  • Chatter messages (private messages) can't have attachments - how would you relate the two to each other? There is no relationship available. Commented Jul 10, 2014 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? Commented Jul 10, 2014 at 20:50

2 Answers 2

9

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;
}
4
  • This looks good. So are Chatter files just content documents? Commented Jul 10, 2014 at 21:48
  • Content Versions. And FeedItem and FeedComment records relate to them. Commented Jul 10, 2014 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
    Commented Nov 1, 2014 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. Commented Nov 1, 2014 at 15:56
3

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;

You must log in to answer this question.

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