I want to post chatter items via Apex with a link attached and an @ mention. I've been able to figure out how to attach the link and @ mention through research.

The next step to finishing this is to change the CreatedById of the post - Instead of it saying MyName to FriendName, I want it to say AnotherUser to FriendName.

Doing this using the FeedItem object is relatively easy since you can change the CreatedById value but I'm having trouble figuring out how I can do that with the ConnectApi.

Here is my code so far :

ConnectApi.FeedType feedType = ConnectApi.FeedType.UserProfile;
ConnectApi.FeedItemInput feedItemInput = new ConnectApi.FeedItemInput();
feedItemInput.subjectId = 'Friends Id'

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

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

textSegmentInput.text = 'Congrats ';
mentionSegmentInput.id = 'Friends Id';

ConnectApi.LinkCapabilityInput linkInput = new ConnectApi.LinkCapabilityInput();
linkInput.url = 'www.helloworld.com';
linkInput.urlName = 'hello world';

ConnectApi.FeedElementCapabilitiesInput feedElementCapabilitiesInput = new ConnectApi.FeedElementCapabilitiesInput();
feedElementCapabilitiesInput.link = linkInput;

feedItemInput.capabilities = feedElementCapabilitiesInput;
feedItemInput.body = messageBodyInput;
ConnectApi.FeedElement feedElement = ConnectApi.ChatterFeeds.postFeedElement(Network.getNetworkId(), feedItemInput);

Open to other approaches too

Your Answer

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

Browse other questions tagged or ask your own question.