function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
clouduserclouduser 

any idea to share the record to the user whom we mentioned @someone in the chatter posting.

Any thoughts to share the record to the user whom we mentioned @someone in the chatter posting. I know we can do somethng with the feeditem trigger.

 

Best Answer chosen by Admin (Salesforce Developers) 
clouduserclouduser

thanks for the suggestion. I am able to achieve this by writing a trigger on feeditem using connectapi apex classes. find the sample code to retrieve the user ids of @mentioned users.

ConnectApi.FeedItem feedItem = ConnectApi.ChatterFeeds.getFeedItem(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.user.id);
}
}

All Answers

Bhawani SharmaBhawani Sharma
SFDC doesn't store the user Id in feedItem past when using @mention. So you will have to dependent on Custom Parsing. In trigger code, you will have to parse the feedItem body and split user's name from that. Once getting user full name, do a SOQL query to fetch the user id and then create a sharing record.
One issue with this approach is, if there are 2 users with the same name, you can not identify which one was in @mention.
clouduserclouduser

thanks for the suggestion. I am able to achieve this by writing a trigger on feeditem using connectapi apex classes. find the sample code to retrieve the user ids of @mentioned users.

ConnectApi.FeedItem feedItem = ConnectApi.ChatterFeeds.getFeedItem(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.user.id);
}
}

This was selected as the best answer
Bhawani SharmaBhawani Sharma
Awesome, Thanks for sharing. What this communityId variable is?
clouduserclouduser

String communityId = null;

 

refer the API documentation for more details..

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_connect_api.htm