Newer Version Available

This content describes an older version of this product. View Latest

Post a Batch of Feed Elements

Use a trigger to call a method to bulk post to the feeds of accounts.
This trigger calls postFeedElementBatch(communityId, feedElements) to bulk post to the feeds of newly inserted accounts.
1trigger postFeedItemToAccount 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.BatchInput batchInput = new ConnectApi.BatchInput(input);
23        batchInputs.add(batchInput);
24    }
25
26    ConnectApi.ChatterFeeds.postFeedElementBatch(Network.getNetworkId(), batchInputs);
27}