Find your content:

Search form

You are here

How can I add an @mention when creating a FeedItem via APEX?

 
Share

I have found that this can be done via the Chatter API, and that there is a pilot program (http://developer.force.com/cookbook/recipe/connect-in-apex-pilot) to bring this to APEX, but I am not familiar at all with REST API. Is there a workaround that will allow me to add FeedItems with @mentions now?

Thanks


Attribution to: jackerman09

Possible Suggestion/Solution #1

Connect in Apex is now called Chatter in Apex will be generally available in the Summer '13 release. You can read more about it in the documentation. Here's a simple code snippet that shows you how to @-mention someone using Chatter in Apex.

// Post a feed item that has an @-mention.

String communityId = null;
ConnectApi.FeedType feedType = ConnectApi.FeedType.UserProfile;
String userToMention = '005xx000001TDn3'
String subjectId = '005xx000001TDn3';

ConnectApi.MessageBodyInput messageInput = new ConnectApi.MessageBodyInput();
messageInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();

ConnectApi.TextSegmentInput textSegment = new ConnectApi.TextSegmentInput();
textSegment.text = 'Hey there ';
messageInput.messageSegments.add(textSegment);

ConnectApi.MentionSegmentInput mentionSegment = new ConnectApi.MentionSegmentInput();
mentionSegment.id = userToMention;
messageInput.messageSegments.add(mentionSegment);

textSegment = new ConnectApi.TextSegmentInput();
textSegment.text = '. How are you?';
messageInput.messageSegments.add(textSegment);

ConnectApi.FeedItemInput input = new ConnectApi.FeedItemInput();
input.body = messageInput;

ConnectApi.FeedItem feedItemRep = ConnectApi.ChatterFeeds.postFeedItem(communityId, feedType, subjectId, input, null);

In addition, we've provided a helper Apex class that you can use to make it easier to post mentions. You can get it from github, and there's a blog post about it too:

https://github.com/alouie-sfdc/ConnectApiHelper

http://www.bridgefarmconsulting.com/blog/new-helper-classes-for-working-with-the-connectapi/


Attribution to: alouie

Possible Suggestion/Solution #2

There is no other way other than invoking the chatter rest Api to post a chatter mention.

String salesforceHost = System.Url.getSalesforceBaseURL().toExternalForm();
        
String url =  salesforceHost + '/services/data/v23.0/chatter/feeds/record/' + RecordId + '/feed-items';
            
HttpRequest req = new HttpRequest();
            
req.setMethod('POST');
req.setEndpoint(url);
req.setHeader('Content-type', 'application/json');
req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
            
req.setBody('{ "body" : { "messageSegments" : [ { "type": "mention", "id" : "' + UserId + '" }, { "type": "text",  "text" : "' + ' ' + chatterAlert +  '" } ] } }');
Http http = new Http();
  
HTTPResponse res = http.send(req);

Attribution to: techtrekker

Possible Suggestion/Solution #3

You already know this, but if anyone else has this question, you can use Chatter in Apex to post a feed item with an @-mention using the method: ConnectApi.ChatterFeeds.postFeedItem()

Doc is in the Chatter in Apex section here: http://www.salesforce.com/us/developer/docs/apexcode/index.htm


Attribution to: Chris S

Possible Suggestion/Solution #4

I am not aware of a way that automates it. You can look through the developerforce documentation on Chatter to double check, but I didn't see anything in it.

Using the chrome developer console, I examined the http request that SF sends when you post a comment with a @mention in it.

The ajax post of a normal chatter post of @[Peter Knolle] - Hey there! on an Account record posts a param to /chatter/handlers/feeditems of text={@005d0000000xhgo} - Hey there! in addition to other stuff, so I guess that the standard Salesforce chatter code parses that ID out to create the link and creates relevant posts in the referenced objects' feeds. As you said, it'd be nice to have the apex engine handle that behind the scenes by parsing out the FeedItem body and creating relevant posts (e.g., able to do post.Body='{@005d0000000xhgo} - Hey there!' on a FeedItem post and have SF linkify it and create the related object post.

So, I think that you'd have to manually create the link for the @mention and the other posts for the @mentioned objects. For example if you @mention a user then a post is made to their user profile as well as the object (e.g., Account) that you are creating the post for. If you are creating the feed post via Apex without any user input you should know what type of objects are being used and how to get their Ids to create the link; however if you are taking text input from the user then you wouldn't have a way to know that.


Attribution to: Peter Knolle
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/1545

My Block Status

My Block Content