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?


  • 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. Commented Sep 25, 2012 at 0:05
  • Yeah, no pure-Apex workaround yet.
    – zachelrath
    Commented Sep 29, 2012 at 14:21
  • This has been in Apex since 2013. See answers below.
    – pchittum
    Commented Oct 3, 2014 at 8:25

4 Answers 4


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.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);

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 ';

ConnectApi.MentionSegmentInput mentionSegment = new ConnectApi.MentionSegmentInput();
mentionSegment.id = userToMention;

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

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:



  • This is very useful. Helped me a lot.
    – Patlatus
    Commented May 20, 2014 at 10:16

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.


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

  • Does this support bulkified operations ?
    – PK85
    Commented Jun 5, 2013 at 16:56
  • no, sorry it does not
    – Chris S
    Commented Jun 12, 2013 at 3:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .