You need to sign in to do that
Don't have an account?

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:
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); } }
You code looks fine, just need to query the case in your method to fetch owner Id and Id of the case record.
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.
All Answers
You code looks fine, just need to query the case in your method to fetch owner Id and Id of the case record.
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.