How to Upload a Chatter File Using Apex?

Question
In the Chatter tab, I can navigate to “Files” and upload a file without attaching it to any record. I want to achieve the same functionality using Apex. How can I create a Chatter File via Apex?
Answer
To upload a file in Apex, you need to work with the ContentVersion
object. Unlike the Attachment
object, ContentVersion
is used for Salesforce Files and supports Chatter.
Boost your Salesforce career with CRS Info Solutions’ expert-led Salesforce training, hands-on projects, and a free demo—perfect for beginners and professionals!!!
The following Apex method creates a standalone file in Salesforce Files:
public static ContentVersion generateContentVersionFile(Boolean doInsert) {
return generateNewContentVersion(null, doInsert);
}
public static ContentVersion generateNewContentVersion(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;
}
Explanation
The generateContentVersionFile
method creates a new Salesforce File (ContentVersion
) by calling generateNewContentVersion
, which sets the file’s title, path, and binary data. If a ContentDocumentId
is provided, it associates the new version with an existing file; otherwise, it creates a standalone file. The doInsert
flag determines whether the record is inserted into the database.
If you also want to create a Chatter post with the uploaded file, you can relate the file to a FeedItem
using the RelatedRecordId
field:
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;
}
Explanation
The method generatePostWithRelatedDocument
creates a FeedItem
(Chatter post) with a related file by linking a ContentVersion
record to a specified parent record. It sets the post body, assigns the ParentId
for context (e.g., a user or record), and attaches the file using RelatedRecordId
. Finally, it inserts the FeedItem
into Salesforce and returns the created record.
This approach ensures the file is uploaded and available in Salesforce Files without being attached to a specific record unless needed.
Summing Up
To upload a Chatter File via Apex, use the ContentVersion
object, which allows you to create standalone Salesforce Files without attaching them to a record. The provided Apex methods generate and insert a ContentVersion
file, setting necessary attributes like Title
, PathOnClient
, and VersionData
. Additionally, if you want to associate the file with a Chatter post, you can create a FeedItem
using the RelatedRecordId
field. This approach ensures flexibility in file management while leveraging Salesforce Files instead of the deprecated Attachment
object.
Launch Your Salesforce Career with Expert Training in Australia
Take your career to new heights with professional Salesforce training in Australia from CRS Info Solutions. Designed to equip you with the skills needed to thrive in the fast-growing Salesforce ecosystem, our courses focus on practical learning with real-time project experience. From Salesforce Admin and Developer to cutting-edge AI modules, we cover it all with hands-on training led by industry experts.
Whether you’re a beginner exploring the Salesforce training in Australia or an experienced professional looking to upgrade your skills, our training is tailored to meet your career goals. With personalized support, in-depth course materials, and expert guidance for certification and interview preparation, you’ll be fully prepared to excel in the industry.
Join us today for a free demo session and take the first step towards a rewarding Salesforce career!!!
Related Posts:
Collections in Apex Programming of Salesforce
Apex programming Examples