Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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

share|improve this question
    
I've heard some quality rumors that the features of the chatter REST API are being ported to apex soon. I suspect this isn't possible until a future release is out. –  ca_peterson Sep 25 '12 at 0:05
    
Yeah, no pure-Apex workaround yet. –  zachelrath Sep 29 '12 at 14:21
add comment

4 Answers 4

up vote 9 down vote accepted

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);
share|improve this answer
add comment

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/

share|improve this answer
    
This is very useful. Helped me a lot. –  Patlatus May 20 at 10:16
add comment

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.

share|improve this answer
add comment

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

share|improve this answer
    
Does this support bulkified operations ? –  PK85 Jun 5 '13 at 16:56
    
no, sorry it does not –  Chris S Jun 12 '13 at 3:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.