+ Start a Discussion
JustDJustD 

Losing @ mentions in apex trigger

Hi,

 

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.  

 

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

Neha LundNeha Lund

Just try this

 

trigger FeedItemTagger on FeedItem (before insert) {

          for (FeedItem fi : trigger.new) {

                  if(!fi.body.contains('@'))

                  fi.body = fi.body + ' I added this #tag in my trigger.';

                  }

          }

Sfd developerSfd developer

Hi,

 

Try this,

 

trigger FeedItemTagger on FeedItem (before insert) {

          for (FeedItem fi : trigger.new) {

                  if(!fi.body.startsWith('@'))

                         fi.body = fi.body + ' I added this #tag in my trigger.';

                  }

          }

}

JustDJustD
Thanks.

But This just avoids the problem as I need to be able to add my #tag to a post with an @mention.

What I need to do is add the #tag while preserving the @mention. Something is happening behind the scenes as the value in the Body field of the FeedItem is already plain text by the time it gets to the trigger. And if I change it in any way it loses the @mention. Also I cannot append an @mention to the Body.

Still searching for a solution...
JustDJustD

Thanks.  

 

I posted a more complete reply to Neha's very similar suggestion.

katrinaxxkatrinaxx
Hi! Were you able to work around this issue?