1

I'm trying to do a chatter post and tag a user on a record from Apex Trigger.

I tried the following approach,per https://blog.jeffdouglas.com/2013/10/03/chatter-for-apex-code-examples/

Id userId = userinfo.getUserId();
Id userToMentionId = userinfo.getUserId();
string postText = 'test post';

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

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

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

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

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

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

It says that that the method is removed, Error below,

Method was removed after version 31.0: postFeedItem

I tried invoking the Chatter Rest Api for post and tagging user, it worked but I dont want a webservice approach. Please guide

Webservice :

String salesforceHost = System.Url.getSalesforceBaseURL().toExternalForm();
        
        String url = salesforceHost + '/services/data/v33.0/chatter/feed-elements';
        system.debug('Url generated: '+url);
        
        HttpRequest req = new HttpRequest();
        
        req.setMethod('POST');
        req.setEndpoint(url);
        req.setHeader('Content-type', 'application/json');
        req.setHeader('Authorization', 'OAuth ' + sesId);
        
        req.setBody('{"body": {"messageSegments": [{"type": "mention", "id": "' + UserId + '"}, {"type": "text", "text": "' + ' ' + ChatterPostText + '"} ] }, "feedElementType": "FeedItem", "subjectId": "' + RecordId + '", "visibility": "AllUsers"}');
        Http http = new Http();
        
        HTTPResponse res = http.send(req);
        system.debug('Response: '+res.getBody());

2 Answers 2

-3

Post Feed Element can be used to post your ConnectApi.FeedItemInput

1
  • 5
    SF is notorious for killing links.. can you add relevant code snippets so this answer is still relevant even if the link dies? Jan 15, 2021 at 17:08
6

You can work with the ConnectApi ChatterFeeds classes to post directly yourself. Using postFeedElement(communityId, feedElement) should be enough to swap in with your original top code (where you create the ConnectApi.FeedItemInput) and there's other examples in the doc. They have one for mentions as well which is pasted below (one change to make communityId null if it's not a community):

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 = '005RR000000Dme9';
messageBodyInput.messageSegments.add(mentionSegmentInput);

textSegmentInput.text = 'Could you take a look?';
messageBodyInput.messageSegments.add(textSegmentInput);

feedItemInput.body = messageBodyInput;
feedItemInput.feedElementType = ConnectApi.FeedElementType.FeedItem;
feedItemInput.subjectId = '0F9RR0000004CPw'; //swap in your record/User Id depending on where this post is supposed to go

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

Just as a FYI - there's also a ConnectApiHelper repository which can make this less verbose, but unless you have a heavy code use case for posting/working with chatter you might not need it.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .