Newer Version Available

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

Edit a Comment

This example calls updateComment(communityId, commentId, comment) to edit a comment.
1swfobject.registerObject("clippy.codeblock-0", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17String commentId;
18String communityId = Network.getNetworkId();
19
20// Get the last feed item created by the current user.
21List<FeedItem> feedItems = [SELECT Id FROM FeedItem WHERE CreatedById = :UserInfo.getUserId() ORDER BY CreatedDate DESC];
22if (feedItems.isEmpty()) {
23    // Return null within anonymous apex.
24    return null;
25}
26String feedElementId = feedItems[0].id;
27
28ConnectApi.CommentPage commentPage = ConnectApi.ChatterFeeds.getCommentsForFeedElement(communityId, feedElementId);
29if (commentPage.items.isEmpty()) {
30    // Return null within anonymous apex.
31    return null;
32}
33commentId = commentPage.items[0].id;
34
35ConnectApi.FeedEntityIsEditable isEditable = ConnectApi.ChatterFeeds.isCommentEditableByMe(communityId, commentId);
36
37if (isEditable.isEditableByMe == true){
38    ConnectApi.CommentInput commentInput = new ConnectApi.CommentInput();
39    ConnectApi.MessageBodyInput messageBodyInput = new ConnectApi.MessageBodyInput();
40    ConnectApi.TextSegmentInput textSegmentInput = new ConnectApi.TextSegmentInput();
41
42    messageBodyInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();
43
44    textSegmentInput.text = 'This is my edited comment.';
45    messageBodyInput.messageSegments.add(textSegmentInput);
46
47    commentInput.body = messageBodyInput;
48
49    ConnectApi.Comment editedComment = ConnectApi.ChatterFeeds.updateComment(communityId, commentId, commentInput);
50}
51