Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.

Post a Rich-Text Feed Element with a Code Block

Call a method to post a feed element with a code block.
Call postFeedElement(communityId, feedElement) to post a feed item with a code block.
1String communityId = null;
2String targetUserOrGroupOrRecordId  = 'me';
3String codeSnippet = '<html>\n\t<body>\n\t\tHello, world!\n\t</body>\n</html>';
4ConnectApi.FeedItemInput input = new ConnectApi.FeedItemInput();
5input.subjectId = targetUserOrGroupOrRecordId;
6input.feedElementType = ConnectApi.FeedElementType.FeedItem;
7
8ConnectApi.MessageBodyInput messageInput = new ConnectApi.MessageBodyInput();
9ConnectApi.TextSegmentInput textSegment;
10ConnectApi.MarkupBeginSegmentInput markupBeginSegment;
11ConnectApi.MarkupEndSegmentInput markupEndSegment;
12
13messageInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();
14
15markupBeginSegment = new ConnectApi.MarkupBeginSegmentInput();
16markupBeginSegment.markupType = ConnectApi.MarkupType.Code;
17messageInput.messageSegments.add(markupBeginSegment);
18
19textSegment = new ConnectApi.TextSegmentInput();
20textSegment.text = codeSnippet;
21messageInput.messageSegments.add(textSegment);
22
23markupEndSegment = new ConnectApi.MarkupEndSegmentInput();
24markupEndSegment.markupType = ConnectApi.MarkupType.Code;
25messageInput.messageSegments.add(markupEndSegment);
26
27input.body = messageInput;
28
29ConnectApi.ChatterFeeds.postFeedElement(communityId, input);