Newer Version Available
Post a Comment with an Existing File
To post a comment and attach an existing file (already uploaded to Salesforce) to the
comment, create a ConnectApi.CommentInput object
to pass to postCommentToFeedElement(communityId, feedElementId, comment, feedElementFileUpload).
1String feedElementId = '0D5D0000000KtW3';
2
3ConnectApi.CommentInput commentInput = new ConnectApi.CommentInput();
4
5ConnectApi.MessageBodyInput messageBodyInput = new ConnectApi.MessageBodyInput();
6ConnectApi.TextSegmentInput textSegmentInput = new ConnectApi.TextSegmentInput();
7
8textSegmentInput.text = 'I attached this file from Salesforce Files.';
9
10messageBodyInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();
11messageBodyInput.messageSegments.add(textSegmentInput);
12commentInput.body = messageBodyInput;
13
14ConnectApi.CommentCapabilitiesInput commentCapabilitiesInput = new ConnectApi.CommentCapabilitiesInput();
15ConnectApi.ContentCapabilityInput contentCapabilityInput = new ConnectApi.ContentCapabilityInput();
16
17commentCapabilitiesInput.content = contentCapabilityInput;
18contentCapabilityInput.contentDocumentId = '069D00000001rNJ';
19
20commentInput.capabilities = commentCapabilitiesInput;
21
22ConnectApi.Comment commentRep = ConnectApi.ChatterFeeds.postCommentToFeedElement(Network.getNetworkId(), feedElementId, commentInput, null);