How to get @mentioned user from Feed Item in Salesforce?

Sample Code:

String communityId = null;
String feedItemId = '0D529000001ABmQCAW';//Sample FeedItem Id

ConnectApi.FeedElement feedItem = ConnectApi.ChatterFeeds.getFeedElement(communityId, feedItemId);
List<ConnectApi.MessageSegment> messageSegments = feedItem.body.messageSegments;
for (ConnectApi.MessageSegment messageSegment : messageSegments) {
    if (messageSegment instanceof ConnectApi.MentionSegment) {
        ConnectApi.MentionSegment mentionSegment = (ConnectApi.MentionSegment) messageSegment;
        System.debug('Mentioned user name: ' + mentionSegment.name);
        System.debug('Mentioned user id: ' + mentionSegment.record);
    }
}

Cheers!!!

No comments:

Post a Comment