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

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

ConnectApi.MentionSegment in Salesforce Apex can be used to find @mentioned user from Feed Item.

Sample Feed Item:

Sample Code:

String communityId = null;
for ( FeedItem objFI : [ 
    SELECT Id 
    FROM FeedItem 
    WHERE CreatedDate = TODAY 
] ) {
    
    ConnectApi.FeedElement feedItem = ConnectApi.ChatterFeeds.getFeedElement(
        communityId, objFI.Id
    );
    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
            );
            
        }
        
    }
    
}

Output:

Leave a Reply