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 Batch of Feed Elements with a New (Binary) File

Use a trigger to call a method to bulk post a new file to the feeds of accounts.

This example is valid in version 32.0–35.0. In version 36.0 and later, you can’t post a batch of feed elements with a new file in the same call. Upload the file to Salesforce first, and then specify the uploaded file when posting a batch of feed elements.

Important

This trigger calls postFeedElementBatch(communityId, feedElements) to bulk post to the feeds of newly inserted accounts. Each post has a new file (binary) attachment.
1trigger postFeedItemToAccountWithBinary on Account (after insert) {
2    Account[] accounts = Trigger.new;
3    
4    // Bulk post to the account feeds.
5
6    List<ConnectApi.BatchInput> batchInputs = new List<ConnectApi.BatchInput>();
7
8    for (Account a : accounts) {
9        ConnectApi.FeedItemInput input = new ConnectApi.FeedItemInput();
10
11        input.subjectId = a.id;
12        
13        ConnectApi.MessageBodyInput body = new ConnectApi.MessageBodyInput();
14        body.messageSegments = new List<ConnectApi.MessageSegmentInput>();
15
16        ConnectApi.TextSegmentInput textSegment = new ConnectApi.TextSegmentInput();
17        textSegment.text = 'Let\'s win the ' + a.name + ' account.';
18
19        body.messageSegments.add(textSegment);
20        input.body = body;
21
22        ConnectApi.ContentCapabilityInput contentInput = new ConnectApi.ContentCapabilityInput();
23        contentInput.title = 'Title';
24
25        ConnectApi.FeedElementCapabilitiesInput capabilities = new ConnectApi.FeedElementCapabilitiesInput();
26        capabilities.content = contentInput;
27
28        input.capabilities = capabilities;
29
30        String text = 'We are words in a file.';
31        Blob myBlob = Blob.valueOf(text);
32        ConnectApi.BinaryInput binInput = new ConnectApi.BinaryInput(myBlob, 'text/plain', 'fileName');
33
34        ConnectApi.BatchInput batchInput = new ConnectApi.BatchInput(input, binInput);
35
36        batchInputs.add(batchInput);
37    }
38
39    ConnectApi.ChatterFeeds.postFeedElementBatch(Network.getNetworkId(), batchInputs);