In this post, we will implement the functionality to post to Chatter from Apex using FeedItem. We can easily post to chatter from Flow and Process Builder.
But sometimes it is required to do it using Apex due to the critical business requirement. And if that is the case for you, you are at the right place. This implementation will also cover making a chatter post to a particular user or to any object record like Account.
Let’s get into the implementation. This will be a real quick one.
In order to post to Chatter from Apex, the first thing that we need to do is to create an instance of FeedItem Class.
Then, we need to assign below required parameters to the instance of FeedItem:
- ParentId: This is the Id of object to which the FeedItem is related to. We can put User Id here to post to someone’s Chatter feed. We can also use Id of other Objects like Account to post to that specific record. Please find the full list of supported objects in official Salesforce documentation here.
- Body: The body or the content of the post.
Then we just need to insert the record.
Post to Chatter from Apex using FeedItem
This is how our code will look:
FeedItem objPost = new FeedItem(); objPost.ParentId = '0055g00000BPDMe'; objPost.Body = 'Hello there from APEX'; insert objPost;
And this is how our post would look like:
That is all. That’s how simple it is to post to Chatter from Apex to the user or any other object using FeedItem.
If you don’t want to miss new implementations, please Subscribe here.
See you in the next implementation, thank you!