function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kon DeleKon Dele 

How to Automatically Add @mention to Chatter Post

How can I via apex automatically @mention the case owner or case contact on the case feed in chatter? 
Using the code example in the developer's guide, here's my class:
public with sharing class AtMentionsUtility {  

    public static void mentionedUser(){           
        
        Case c = new Case();
        ConnectApi.FeedItemInput feedItemInput = new ConnectApi.FeedItemInput();
        ConnectApi.MentionSegmentInput mentionSegmentInput = new ConnectApi.MentionSegmentInput();
        ConnectApi.MessageBodyInput messageBodyInput = new ConnectApi.MessageBodyInput();
        ConnectApi.TextSegmentInput textSegmentInput = new ConnectApi.TextSegmentInput();

        messageBodyInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();

        mentionSegmentInput.id = c.OwnerId;
        messageBodyInput.messageSegments.add(mentionSegmentInput);

        textSegmentInput.text = '';
        messageBodyInput.messageSegments.add(textSegmentInput);

        feedItemInput.body = messageBodyInput;
        feedItemInput.feedElementType = ConnectApi.FeedElementType.FeedItem;
        feedItemInput.subjectId = c.Id;

        ConnectApi.FeedElement feedElement = ConnectApi.ChatterFeeds.postFeedElement(Network.getNetworkId(), feedItemInput, null);
    }
}

 
Best Answer chosen by Kon Dele
Shashikant SharmaShashikant Sharma
Hi Kon,

You code looks fine, just need to query the case in your method to fetch owner Id and Id of the case record.
 
public with sharing class AtMentionsUtility {  

    public static void mentionedUser(Id caseRecordId){           
        
        Case c = [ Select OwnerId From Case Where Id =: caseRecordId];

        ConnectApi.FeedItemInput feedItemInput = new ConnectApi.FeedItemInput();
        ConnectApi.MentionSegmentInput mentionSegmentInput = new ConnectApi.MentionSegmentInput();
        ConnectApi.MessageBodyInput messageBodyInput = new ConnectApi.MessageBodyInput();
        ConnectApi.TextSegmentInput textSegmentInput = new ConnectApi.TextSegmentInput();

        messageBodyInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();

        mentionSegmentInput.id = c.OwnerId;
        messageBodyInput.messageSegments.add(mentionSegmentInput);

        textSegmentInput.text = '';
        messageBodyInput.messageSegments.add(textSegmentInput);

        feedItemInput.body = messageBodyInput;
        feedItemInput.feedElementType = ConnectApi.FeedElementType.FeedItem;
        feedItemInput.subjectId = c.Id;

        ConnectApi.FeedElement feedElement = ConnectApi.ChatterFeeds.postFeedElement(Network.getNetworkId(), feedItemInput, null);
    }
}
Then you could call this method from any other class and it should work fine. In order to test you could run this in developer console with a valid case record id.
AtMentionsUtility.mentionedUser('5000000assMa');



 

All Answers

Shashikant SharmaShashikant Sharma
Hi Kon,

You code looks fine, just need to query the case in your method to fetch owner Id and Id of the case record.
 
public with sharing class AtMentionsUtility {  

    public static void mentionedUser(Id caseRecordId){           
        
        Case c = [ Select OwnerId From Case Where Id =: caseRecordId];

        ConnectApi.FeedItemInput feedItemInput = new ConnectApi.FeedItemInput();
        ConnectApi.MentionSegmentInput mentionSegmentInput = new ConnectApi.MentionSegmentInput();
        ConnectApi.MessageBodyInput messageBodyInput = new ConnectApi.MessageBodyInput();
        ConnectApi.TextSegmentInput textSegmentInput = new ConnectApi.TextSegmentInput();

        messageBodyInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();

        mentionSegmentInput.id = c.OwnerId;
        messageBodyInput.messageSegments.add(mentionSegmentInput);

        textSegmentInput.text = '';
        messageBodyInput.messageSegments.add(textSegmentInput);

        feedItemInput.body = messageBodyInput;
        feedItemInput.feedElementType = ConnectApi.FeedElementType.FeedItem;
        feedItemInput.subjectId = c.Id;

        ConnectApi.FeedElement feedElement = ConnectApi.ChatterFeeds.postFeedElement(Network.getNetworkId(), feedItemInput, null);
    }
}
Then you could call this method from any other class and it should work fine. In order to test you could run this in developer console with a valid case record id.
AtMentionsUtility.mentionedUser('5000000assMa');



 
This was selected as the best answer
Kon DeleKon Dele
How can I get this to work in production? I'm able to test succesfully in the dev console, but can't get this to work by posting text. Do I need a FeedItem trigger to call on the class??