6

How to mention a user in a Chatter Group in an Apex Trigger?

There are similar questions existing in SFSE but they all seem to have outdated answers using old cumbersome APIs. I believe the easiest way is to use the FeedItem object. I have only managed to send a message to a user (not the same as an "@" mention) and share with the company using the below code. Documentation is scanty regarding how to mention a user in a Chatter Group.

FeedItem post = new FeedItem();
post.ParentId = u.Id;
post.Body = 'Please find the download URL herewith.';
post.Title = 'User Guide';
post.LinkUrl = 'https://c.cs7.content.force.com/sfc/servlet.shepherd/version/download/068M0000000CvrF';
feedItemList.add(post);
  • Considering that now with Winter '14 we can also mention Chatter Group names to post to groups, how can we mention multiple items (Chatter Group + User) in an Apex Trigger? – Shumon Saha Oct 29 '13 at 15:24
5

Chatter in apex (ConnectApi) was designed for this type of coding:

ConnectApi.FeedType feedType = ConnectApi.FeedType.UserProfile;

ConnectApi.FeedItemInput input = new ConnectApi.FeedItemInput();
ConnectApi.MessageBodyInput messageInput = new ConnectApi.MessageBodyInput();
ConnectApi.TextSegmentInput textSegment;
ConnectApi.MentionSegmentInput mentionSegment = new ConnectApi.MentionSegmentInput();

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

textSegment = new ConnectApi.TextSegmentInput();
textSegment.text = 'Hey there ';
messageInput.messageSegments.add(textSegment);

mentionSegment.id = '005D0000001LLO1';
messageInput.messageSegments.add(mentionSegment);

textSegment = new ConnectApi.TextSegmentInput();
textSegment.text = '. How are you?';
messageInput.messageSegments.add(textSegment);

input.body = messageInput;

ConnectApi.FeedItem feedItemRep = ConnectApi.ChatterFeeds.postFeedItem(communityId, feedType, 'me', input, null);

In this case the mentionSegment.id would be your group id. Full documentation of the ConnectApi can be found here:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_connect_api.htm

| improve this answer | |

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.