Newer Version Available

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

Post a Rich-Text Comment with Inline Image

You can post rich-text comments with inline images and mentions two ways. Use the ConnectApiHelper repository on GitHub to write a single line of code, or use this example, which calls postCommentToFeedElement(communityId, feedElementId, comment, feedElementFileUpload). In this example, the image file is existing content that has already been uploaded to Salesforce.
1String communityId = null;
2String feedElementId = '0D5R0000000SBEr';
3String imageId = '069R00000000IgQ';
4String mentionedUserId = '005R0000000DiMz'; 
5
6ConnectApi.CommentInput input = new ConnectApi.CommentInput();
7ConnectApi.MessageBodyInput messageInput = new ConnectApi.MessageBodyInput();
8ConnectApi.TextSegmentInput textSegment;
9ConnectApi.MentionSegmentInput mentionSegment;
10ConnectApi.MarkupBeginSegmentInput markupBeginSegment;
11ConnectApi.MarkupEndSegmentInput markupEndSegment;
12ConnectApi.InlineImageSegmentInput inlineImageSegment;
13
14messageInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();
15
16
17markupBeginSegment = new ConnectApi.MarkupBeginSegmentInput();
18markupBeginSegment.markupType = ConnectApi.MarkupType.Bold;
19messageInput.messageSegments.add(markupBeginSegment);
20
21textSegment = new ConnectApi.TextSegmentInput();
22textSegment.text = 'Hello ';
23messageInput.messageSegments.add(textSegment);
24
25mentionSegment = new ConnectApi.MentionSegmentInput();
26mentionSegment.id = mentionedUserId;
27messageInput.messageSegments.add(mentionSegment);
28
29textSegment = new ConnectApi.TextSegmentInput();
30textSegment.text = '!';
31messageInput.messageSegments.add(textSegment);
32
33markupEndSegment = new ConnectApi.MarkupEndSegmentInput();
34markupEndSegment.markupType = ConnectApi.MarkupType.Bold;
35messageInput.messageSegments.add(markupEndSegment);
36
37inlineImageSegment = new ConnectApi.InlineImageSegmentInput();
38inlineImageSegment.altText = 'image one';
39inlineImageSegment.fileId = imageId;
40messageInput.messageSegments.add(inlineImageSegment);
41
42input.body = messageInput;
43
44ConnectApi.ChatterFeeds.postCommentToFeedElement(communityId, feedElementId, input, null);