Post a Rich-Text Feed Element with Inline Image
Call a method or use the ConnectApiHelper repository to post a feed element with an
already uploaded, inline image.
You can post rich-text feed elements 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 postFeedElement(communityId,
feedElement). In this example, the image file is existing content that has
already been uploaded to Salesforce as a content document (069). The post also includes text
and a
mention.
1String communityId = null;
2String imageId = '069D00000001INA';
3String mentionedUserId = '005D0000001QNpr';
4String targetUserOrGroupOrRecordId = '005D0000001Gif0';
5ConnectApi.FeedItemInput input = new ConnectApi.FeedItemInput();
6input.subjectId = targetUserOrGroupOrRecordId;
7input.feedElementType = ConnectApi.FeedElementType.FeedItem;
8
9ConnectApi.MessageBodyInput messageInput = new ConnectApi.MessageBodyInput();
10ConnectApi.TextSegmentInput textSegment;
11ConnectApi.MentionSegmentInput mentionSegment;
12ConnectApi.MarkupBeginSegmentInput markupBeginSegment;
13ConnectApi.MarkupEndSegmentInput markupEndSegment;
14ConnectApi.InlineImageSegmentInput inlineImageSegment;
15
16messageInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();
17
18markupBeginSegment = new ConnectApi.MarkupBeginSegmentInput();
19markupBeginSegment.markupType = ConnectApi.MarkupType.Bold;
20messageInput.messageSegments.add(markupBeginSegment);
21
22textSegment = new ConnectApi.TextSegmentInput();
23textSegment.text = 'Hello ';
24messageInput.messageSegments.add(textSegment);
25
26mentionSegment = new ConnectApi.MentionSegmentInput();
27mentionSegment.id = mentionedUserId;
28messageInput.messageSegments.add(mentionSegment);
29
30textSegment = new ConnectApi.TextSegmentInput();
31textSegment.text = '!';
32messageInput.messageSegments.add(textSegment);
33
34markupEndSegment = new ConnectApi.MarkupEndSegmentInput();
35markupEndSegment.markupType = ConnectApi.MarkupType.Bold;
36messageInput.messageSegments.add(markupEndSegment);
37
38inlineImageSegment = new ConnectApi.InlineImageSegmentInput();
39inlineImageSegment.altText = 'image one';
40inlineImageSegment.fileId = imageId;
41messageInput.messageSegments.add(inlineImageSegment);
42
43input.body = messageInput;
44
45ConnectApi.ChatterFeeds.postFeedElement(communityId, input);