7

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

3 Answers 3

6

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

The following doesn't look like the nicest workaround but should do the trick: http://developer.force.com/cookbook/recipe/posting-a-chatter-mention-via-apex

0

There's now a roundabout way to preserve @mentions in FeedItem and FeedComment triggers if you turn on the feature to edit your Chatter posts and enable it for everyone who will be posting. See the blog post on Preserving Mentions in Triggers.

You must log in to answer this question.

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