---
schema_version: "1"
id: a9c25aba1363c98e5b902d11c668590e09569276f3267f2a90e3eb9a2783073e
title: Post a Rich-Text Feed Comment with a Code Block
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexcode/connectapi_examples_post_comment_richtext_code_snippet.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/connectapi_examples_post_comment_richtext_code_snippet.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: 087e9cffd8743f030faffc064a5b92d64c443fb23d00be50cc7318ad5cd9fcd6
description: Call a method to post a comment with a code block.
---
# Post a Rich-Text Feed Comment with a Code Block

Call a method to post a comment with a code block.

This example calls [postCommentToFeedElement(communityId, feedElementId, comment, feedElementFileUpload)](https://developer.salesforce.com/docs/atlas.en-us.264.0.apexref.meta/apexref/apex_ConnectAPI_ChatterFeeds_static_methods.htm#apex_ConnectAPI_ChatterFeeds_postCommentToFeedElement_2) to post a comment with a code block.

String communityId = null;
String feedElementId = '0D5R0000000SBEr';
String codeSnippet = '<html>\\n\\t<body>\\n\\t\\tHello, world!\\n\\t</body>\\n</html>';

ConnectApi.CommentInput input = new ConnectApi.CommentInput();
ConnectApi.MessageBodyInput messageInput = new ConnectApi.MessageBodyInput();
ConnectApi.TextSegmentInput textSegment;
ConnectApi.MarkupBeginSegmentInput markupBeginSegment;
ConnectApi.MarkupEndSegmentInput markupEndSegment;

messageInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();

markupBeginSegment = new ConnectApi.MarkupBeginSegmentInput();
markupBeginSegment.markupType = ConnectApi.MarkupType.Code;
messageInput.messageSegments.add(markupBeginSegment);

textSegment = new ConnectApi.TextSegmentInput();
textSegment.text = codeSnippet;
messageInput.messageSegments.add(textSegment);

markupEndSegment = new ConnectApi.MarkupEndSegmentInput();
markupEndSegment.markupType = ConnectApi.MarkupType.Code;
messageInput.messageSegments.add(markupEndSegment);

input.body = messageInput;

ConnectApi.ChatterFeeds.postCommentToFeedElement(communityId, feedElementId, input, null);