[SalesForce] Losing @ mentions in apex trigger on FeedItem

I have developped a apex trigger on FeedItem after insert. The purpose is to append an hashtag to the body of the post when certain criteria are met. Everything works very well except that any mention in the body of the post becomes plain text. Existing hashtags and the one I append are treated like hashtags.

I type the following post text

This is a post for @[Derek Hughes] to read.

Without the trigger in operation this creates a post with a proper mention of Derek Hughes (hyperlinked) and shows up on my Chatter Feed:

This is a post for Derek Hughes to read.

With the trigger this creates a post like this:

This is a post for @Derek Hughes to read. I added this #tag in my trigger." 

Where the @Derek Hughes is plain text (but wiht out the square brackets) but the #tag is a proper post hashtag (hyperlinked)

The simplified code is as follows (it appends the tag to all posts and exhibits the problem):

trigger FeedItemTagger on FeedItem (before insert) {
      for (FeedItem fi : trigger.new) {
              fi.body = fi.body + ' I added this #tag in my trigger.';
              }
      }

Has anyone tried something like this before?

Any ideas on how to prevent the @ mentions being converted into text.

Thanks
Derek

Best Answer

Hashtags are parsed out of plain text, but you need to use the Connect (Chatter API) or REST API to post mentions.

I've answered a similar question here, which might help.

Copy the FeedBodyParser Utility Class from http://developer.force.com/cookbook/recipe/connect-in-apex-pilot

Then to post a mention simply use

ConnectApi.FeedItemInput feedItemInput = FeedBodyParser.convertToFeedItemInput('Hello @[Ritesh Aswaney](user:005i0000000nEVV)');      
ConnectApi.ChatterFeeds.postFeedItem(null, ConnectApi.FeedType.News, 'me', feedItemInput, null);
Related Topic