I have a trigger that creates a chatter post after insert or after update for Tasks. However, I also want to @mention the created by user for the task. How Can I add this? Thanks

trigger ActivityHistoryToChatter on Task (after insert, after update) {
for(Task t : trigger.new)
    //if it's now a completed task, and wasn't  before
    //create the new chatter post.
    string what = t.whatID;

    if(what.substring(0,3) == 'a19') 
        The above line lets you control exactly which objects you want to toss this on. Adjust the If statement to the 
        3 digit object code for various elements.


        Cases - 500
        Accounts - 001
        Opportunities - 006

        I STRONGLY suggest you use this if you're going to do a larger, longer demo, as you'll need to chatter enable any 
        object you play with the task history on or this will crash. If you're not playing with Activity History/Tasks anywhere
        else, won't be a big deal.

        if(!t.alreadyPosted__c && t.status == 'Completed')//hasn't been posted, completed
            Task daTask = [select id, description, whoid, activityDate, ownerid, subject from Task where id = :t.id];
            string whoelement = daTask.whoId;
            if (whoelement != NULL)
            if(whoElement.substring(0,3) == '003')//contact
                {Contact who = [select name from Contact where id = :whoelement];
                whoElement = who.name;}
                {Lead who2 = [select name from Lead where id = :whoelement];
                whoElement= who2.name;}
        User owner = [select name from User where id = :daTask.OwnerId];

        daTask.alreadyPosted__c = true; 

        FeedPost OppFeed = new FeedPost();
        string body = t.Subject + ' was completed by ' + owner.name + ' on ';
        body += t.activityDate.Month() + '/' + t.activityDate.Day() + '/' + t.ActivityDate.Year() + '\n';
        if(t.description != null)
            body += 'Comments: ' + t.description;
        OppFeed.body = body;
        oppFeed.parentID = t.whatID;
        oppFeed.linkURL = '/' + t.id;
        oppFeed.title = t.subject;
        insert oppFeed;
  • Unfortunately, you cannot add an @mention in this manner. Everything you do to the FeedPost's Body in Apex, either by creating the FeedPost or altering it in a trigger, will be treated as plain-text and will not be processed for mentions. – ScottW Sep 16 '14 at 19:33

The only way to @mention someone in apex is using the ConnectApi (AKA Chatter in apex) and not by just creating a FeedItem in apex. You'll need to refactor most everything in your code around the posting to Chatter. Here is a code sample of a text post with a mention borrowed from an example that Jeff Douglas has on his blog:

// makes a simple chatter text post to the specified user from the running user 
public static void mentionTextPost(Id userId, Id userToMentionId, String postText) { 

   ConnectApi.MessageBodyInput messageInput = new ConnectApi.MessageBodyInput();
   messageInput.messageSegments = new List();

   // add some text before the mention
   ConnectApi.TextSegmentInput textSegment = new ConnectApi.TextSegmentInput();
   textSegment.text = 'Hey ';

   // add the mention
   ConnectApi.MentionSegmentInput mentionSegment = new ConnectApi.MentionSegmentInput();
   mentionSegment.id = userToMentionId;

   // add the text that was passed
   textSegment = new ConnectApi.TextSegmentInput();
   textSegment.text = postText;

   ConnectApi.FeedItemInput input = new ConnectApi.FeedItemInput();
   input.body = messageInput;

   // post it
   ConnectApi.ChatterFeeds.postFeedItem(null, ConnectApi.FeedType.UserProfile, userId, input, null);


Lastly, it's worth noting that you can only post as the running user. It's not possible to post on anyone else's behalf.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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