Newer Version Available

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

Edit a Feed Element

This example calls updateFeedElement(communityId, feedElementId, feedElement) to edit a feed element. Feed items are the only type of feed element that can be edited.
1String communityId = Network.getNetworkId();
2
3// Get the last feed item created by the context user.
4List<FeedItem> feedItems = [SELECT Id FROM FeedItem WHERE CreatedById = :UserInfo.getUserId() ORDER BY CreatedDate DESC];
5if (feedItems.isEmpty()) {
6    // Return null within anonymous apex.
7    return null;
8}
9String feedElementId = feedItems[0].id;
10
11ConnectApi.FeedEntityIsEditable isEditable = ConnectApi.ChatterFeeds.isFeedElementEditableByMe(communityId, feedElementId);
12
13if (isEditable.isEditableByMe == true){
14    ConnectApi.FeedItemInput feedItemInput = new ConnectApi.FeedItemInput();
15    ConnectApi.MessageBodyInput messageBodyInput = new ConnectApi.MessageBodyInput();
16    ConnectApi.TextSegmentInput textSegmentInput = new ConnectApi.TextSegmentInput();
17
18    messageBodyInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();
19
20    textSegmentInput.text = 'This is my edited post.';
21    messageBodyInput.messageSegments.add(textSegmentInput);
22
23    feedItemInput.body = messageBodyInput;
24
25    ConnectApi.FeedElement editedFeedElement = ConnectApi.ChatterFeeds.updateFeedElement(communityId, feedElementId, feedItemInput);
26}