Is there a way to retrieve the user id of @mention user from chatter post in Apex? We can write a before insert trigger on feeditem, but when we display the body (which has @mention user) it simply displays as text. Is there any workaround for this?
[SalesForce] Retrieve UserId of @mention user from chatter post in Apex
Related Solutions
Chatter in apex (ConnectApi) was designed for this type of coding:
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();
textSegment.text = 'Hey there ';
messageInput.messageSegments.add(textSegment);
mentionSegment.id = '005D0000001LLO1';
messageInput.messageSegments.add(mentionSegment);
textSegment = new ConnectApi.TextSegmentInput();
textSegment.text = '. How are you?';
messageInput.messageSegments.add(textSegment);
input.body = messageInput;
ConnectApi.FeedItem feedItemRep = ConnectApi.ChatterFeeds.postFeedItem(communityId, feedType, 'me', input, null);
In this case the mentionSegment.id would be your group id. Full documentation of the ConnectApi can be found here:
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_connect_api.htm
The only way to @mention someone in apex is using the ConnectApi (AKA Chatter in apex) and not by just creating a FeedItem in apex. You'll need to refactor most everything in your code around the posting to Chatter. Here is a code sample of a text post with a mention borrowed from an example that Jeff Douglas has on his blog:
// makes a simple chatter text post to the specified user from the running user
public static void mentionTextPost(Id userId, Id userToMentionId, String postText) {
ConnectApi.MessageBodyInput messageInput = new ConnectApi.MessageBodyInput();
messageInput.messageSegments = new List();
// add some text before the mention
ConnectApi.TextSegmentInput textSegment = new ConnectApi.TextSegmentInput();
textSegment.text = 'Hey ';
messageInput.messageSegments.add(textSegment);
// add the mention
ConnectApi.MentionSegmentInput mentionSegment = new ConnectApi.MentionSegmentInput();
mentionSegment.id = userToMentionId;
messageInput.messageSegments.add(mentionSegment);
// add the text that was passed
textSegment = new ConnectApi.TextSegmentInput();
textSegment.text = postText;
messageInput.messageSegments.add(textSegment);
ConnectApi.FeedItemInput input = new ConnectApi.FeedItemInput();
input.body = messageInput;
// post it
ConnectApi.ChatterFeeds.postFeedItem(null, ConnectApi.FeedType.UserProfile, userId, input, null);
}
Lastly, it's worth noting that you can only post as the running user. It's not possible to post on anyone else's behalf.
- With credit to Jeff, posted on his blog here, Chatter for Apex Code Examples
Best Answer
You can use the new Chatter API in Apex to do this. It's in developer preview currently and is on by default in all Developer Edition orgs. (Note: It went GA in Summer '13.)
Here's an example of how to use it:
Link: API documentation for the ConnectApi.ChatterFeeds class.