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 Comment with a Mention

Make call or use the ConnectApiHelper repository to post a comment with a mention.
You can post comments with 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).
1String communityId = null;
2String feedElementId = '0D5D0000000KtW3';
3
4ConnectApi.CommentInput commentInput = new ConnectApi.CommentInput();
5ConnectApi.MentionSegmentInput mentionSegmentInput = new ConnectApi.MentionSegmentInput();
6ConnectApi.MessageBodyInput messageBodyInput = new ConnectApi.MessageBodyInput();
7ConnectApi.TextSegmentInput textSegmentInput = new ConnectApi.TextSegmentInput();
8
9messageBodyInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();
10
11textSegmentInput.text = 'Does anyone in this group have an idea? ';
12messageBodyInput.messageSegments.add(textSegmentInput);
13
14mentionSegmentInput.id = '005D00000000oOT';
15messageBodyInput.messageSegments.add(mentionSegmentInput);
16
17commentInput.body = messageBodyInput;
18
19ConnectApi.Comment commentRep = ConnectApi.ChatterFeeds.postCommentToFeedElement(communityId, feedElementId, commentInput, null);