Newer Version Available
Edit a Comment
Call a method to edit a comment.
Call updateComment(communityId,
commentId, comment) to edit a
comment.
1String commentId;
2String communityId = Network.getNetworkId();
3
4// Get the last feed item created by the context user.
5List<FeedItem> feedItems = [SELECT Id FROM FeedItem WHERE CreatedById = :UserInfo.getUserId() ORDER BY CreatedDate DESC];
6if (feedItems.isEmpty()) {
7 // Return null within anonymous apex.
8 return null;
9}
10String feedElementId = feedItems[0].id;
11
12ConnectApi.CommentPage commentPage = ConnectApi.ChatterFeeds.getCommentsForFeedElement(communityId, feedElementId);
13if (commentPage.items.isEmpty()) {
14 // Return null within anonymous apex.
15 return null;
16}
17commentId = commentPage.items[0].id;
18
19ConnectApi.FeedEntityIsEditable isEditable = ConnectApi.ChatterFeeds.isCommentEditableByMe(communityId, commentId);
20
21if (isEditable.isEditableByMe == true){
22 ConnectApi.CommentInput commentInput = new ConnectApi.CommentInput();
23 ConnectApi.MessageBodyInput messageBodyInput = new ConnectApi.MessageBodyInput();
24 ConnectApi.TextSegmentInput textSegmentInput = new ConnectApi.TextSegmentInput();
25
26 messageBodyInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();
27
28 textSegmentInput.text = 'This is my edited comment.';
29 messageBodyInput.messageSegments.add(textSegmentInput);
30
31 commentInput.body = messageBodyInput;
32
33 ConnectApi.Comment editedComment = ConnectApi.ChatterFeeds.updateComment(communityId, commentId, commentInput);
34}