How to post message on Chatter using Apex

Hi guys. Today i am going to share how we can post messages on Chatter using Apex code. This could be helpful when a user performed some action in Salesforce, they a post should be created on Chatter with the details so that other users can also get alerts. To do this we have to use FeedItem class which is responsible for creating Chatter Posts.

Code for creating chatter post on behalf of a particular Account is :

FeedItem feed = new FeedItem();
feed.ParentId = "Account Id";
feed.Body = "Message which you want to post";
insert feed;

Once you call this method with the parentId as the User’s account Id and Body as the Message, it will automatically creates a Chatter post.

Originally published at Er.Tarandeep.

Like what you read? Give Er Tarandeep Singh a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.