@mention using ConnectApi

Apex Nov 23, 2014

To @ mention using HttpCallouts check out my post

Another way to @mention would be using Chatter REST API as follows

Use Case: Posting an @mention to a User Profile Feed

Methods Used: ConnectApi.ChatterFeeds.postFeedItem

String communityId = null; // community whih may be customer/partner community

// Initialisation
ConnectApi.FeedType feedType = ConnectApi.FeedType.UserProfile;
ConnectApi.FeedItemInput input = new ConnectApi.FeedItemInput();
ConnectApi.MessageBodyInput messageInput = new ConnectApi.MessageBodyInput();
ConnectApi.TextSegmentInput textSegment;
ConnectApi.MentionSegmentInput mentionSegment = new ConnectApi.MentionSegmentInput();
messageInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();
textSegment = new ConnectApi.TextSegmentInput();

// assigning sample text
textSegment.text = 'Hey there ';
messageInput.messageSegments.add(textSegment);
mentionSegment.id = '005D0000001LLO1'; // sample Id may be User/RecordId
messageInput.messageSegments.add(mentionSegment);
textSegment = new ConnectApi.TextSegmentInput();

// assigning another sample text
textSegment.text = '. How are you?';
messageInput.messageSegments.add(textSegment);
input.body = messageInput;

// Visibility for "Alluser" by Default it would be for internal Users Only
input.visibility = ConnectApi.FeedItemVisibilityType.AllUsers;

// posting feed
ConnectApi.FeedItem feedItemRep = ConnectApi.ChatterFeeds.postFeedItem(communityId, feedType, 'me', input, null);

For more info, please refer Chatter REST API

Phanindra Mangipudi

Salesforce, Lightning Web Componets, Node.Js, Angular 2+, Bootstrap