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
Michael MMichael M 

Add createdbyid to chatter apex post

Hello, I need to set the createdbyid of a chatter post being made from apex. How do I do so? 

    Contact c = [ Select id, related_referral__r.ownerid, related_referral__r.name From Contact Where Id =: contactRecordId];

        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.related_referral__r.ownerid;
        messageBodyInput.messageSegments.add(mentionSegmentInput);

        textSegmentInput.text = ' This contact, the next of kin of patient "' + c.related_referral__r.name +'", has responded to your text and shown interest. Please follow up with them as soon as you cannnnnnnn.';
        messageBodyInput.messageSegments.add(textSegmentInput);

        feedItemInput.body = messageBodyInput;
        feedItemInput.feedElementType = ConnectApi.FeedElementType.FeedItem;
        feedItemInput.subjectId = c.Id;
     
    
ConnectApi.ChatterFeeds.postFeedElement(Network.getNetworkId(), feedItemInput);
Best Answer chosen by Michael M
VinayVinay (Salesforce Developers) 
Hi Michael,

ConnectApi might not support createdbyid in chatter post.

Review below links.

https://developer.salesforce.com/forums/?id=906F00000009AEhIAM
https://salesforce.stackexchange.com/questions/102654/trying-to-post-a-chatter-feed-item-with-mention-and-as-someone-else

Thanks,
Vinay Kumar

All Answers

Agustin BAgustin B
Hi Michael, long time no see, I see you had to change to connect api because of the mentions.
Sadly using Connect api you can only post feed items as the current logged-in user.
Check this for reference: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_connectapi_differences.htm

Hope you find another way to achieve your requirement.
Michael MMichael M
Hi Agustin, lol, yes. Do you think I can combine them? The Trigger cannot @mention, and the connect api cannot set the createdbyid. What if the createdbyid is set in the trigger, then the class is called before insert and sets the mention and body in the connect api?
Agustin BAgustin B
I dont believe you can because connect api does not run in system mode.
You can try but I dont see it working.

Good luck and if it helped you please mark as correct so the question is solved
VinayVinay (Salesforce Developers) 
Hi Michael,

ConnectApi might not support createdbyid in chatter post.

Review below links.

https://developer.salesforce.com/forums/?id=906F00000009AEhIAM
https://salesforce.stackexchange.com/questions/102654/trying-to-post-a-chatter-feed-item-with-mention-and-as-someone-else

Thanks,
Vinay Kumar
This was selected as the best answer