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
the postCommentToFeedElement(communityId, feedElementId, comment, feedElementFileUpload)method.
1swfobject.registerObject("clippy.codeblock-0", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17String feedElementId = '0D5D0000000KtW3';
18
19ConnectApi.CommentInput commentInput = new ConnectApi.CommentInput();
20
21ConnectApi.MessageBodyInput messageBodyInput = new ConnectApi.MessageBodyInput();
22ConnectApi.TextSegmentInput textSegmentInput = new ConnectApi.TextSegmentInput();
23
24textSegmentInput.text = 'I attached this file from Salesforce Files.';
25
26messageBodyInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();
27messageBodyInput.messageSegments.add(textSegmentInput);
28commentInput.body = messageBodyInput;
29
30ConnectApi.CommentCapabilitiesInput commentCapabilitiesInput = new ConnectApi.CommentCapabilitiesInput();
31ConnectApi.ContentCapabilityInput contentCapabilityInput = new ConnectApi.ContentCapabilityInput();
32
33commentCapabilitiesInput.content = contentCapabilityInput;
34contentCapabilityInput.contentDocumentId = '069D00000001rNJ';
35
36commentInput.capabilities = commentCapabilitiesInput;
37
38ConnectApi.Comment commentRep = ConnectApi.ChatterFeeds.postCommentToFeedElement(Network.getNetworkId(), feedElementId, commentInput, null);
39
40