Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.

Post a Feed Element with Existing Files

Call a method to post a feed element with already uploaded files.
Call postFeedElement(communityId, feedElement) to post a feed item with files that have already been uploaded.
1// Define the FeedItemInput object to pass to postFeedElement
2ConnectApi.FeedItemInput feedItemInput = new ConnectApi.FeedItemInput();
3feedItemInput.subjectId = 'me';
4
5ConnectApi.TextSegmentInput textSegmentInput = new ConnectApi.TextSegmentInput();
6textSegmentInput.text = 'Would you please review these docs?';
7
8// The MessageBodyInput object holds the text in the post
9ConnectApi.MessageBodyInput messageBodyInput = new ConnectApi.MessageBodyInput();
10messageBodyInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();
11messageBodyInput.messageSegments.add(textSegmentInput);
12feedItemInput.body = messageBodyInput;
13
14// The FeedElementCapabilitiesInput object holds the capabilities of the feed item.
15// For this feed item, we define a files capability to hold the file(s).
16
17List<String> fileIds = new List<String>();
18fileIds.add('069xx00000000QO');
19fileIds.add('069xx00000000QT');
20fileIds.add('069xx00000000Qn');
21fileIds.add('069xx00000000Qi');
22fileIds.add('069xx00000000Qd');
23
24ConnectApi.FilesCapabilityInput filesInput = new ConnectApi.FilesCapabilityInput();
25filesInput.items = new List<ConnectApi.FileIdInput>();
26
27for (String fileId : fileIds) {
28    ConnectApi.FileIdInput idInput = new ConnectApi.FileIdInput();
29    idInput.id = fileId;
30    filesInput.items.add(idInput);
31}
32
33ConnectApi.FeedElementCapabilitiesInput feedElementCapabilitiesInput = new ConnectApi.FeedElementCapabilitiesInput();
34feedElementCapabilitiesInput.files = filesInput;
35
36feedItemInput.capabilities = feedElementCapabilitiesInput;
37
38// Post the feed item. 
39ConnectApi.FeedElement feedElement = ConnectApi.ChatterFeeds.postFeedElement(Network.getNetworkId(), feedItemInput);