How to Post on Chatter Using Apex

Posting on Chatter from an Apex class allows you to share information and collaborate with users in your Salesforce org. In this article, we will discuss the steps to post on Chatter using Apex, along with the necessary code snippets.

Create an Apex Class

The first step is to create an Apex class that will handle the posting functionality. This class should have a method to post the feed item on Chatter. Here’s an example of how the Apex class can be structured:

apex

public class ChatterPost {
    public String body { get; set; }
    public String userID { get; set; }

    public void postFeed() {
        FeedItem post = new FeedItem();
        post.ParentId = userID;
        post.Body = body;
        insert post;
    }
}

Use the FeedItem Object

In the Apex class, use the FeedItem object to create a new feed item. Set the necessary properties such as the parent ID and the body of the post. The parent ID can be the ID of the user or record you want to post on behalf of. Here’s an example of how to use the FeedItem object:

apex

FeedItem post = new FeedItem();
post.ParentId = userID;
post.Body = body;

Insert the Feed Item

After setting the properties of the feed item, insert it using the `insert` statement. This will create the post on Chatter. Here’s an example of how to insert the feed item:

apex

insert post;

Implement the Visualforce Page

Create a Visualforce page that will allow users to enter the necessary information for the post, such as the user ID and the content of the post. Use input fields and a command button to trigger the posting action. Here’s an example of how the Visualforce page can be structured:

html

 

Enter User ID
Enter Post Content


 



Test the Functionality

Once you have implemented the Apex class and Visualforce page, test the functionality by entering the required information and clicking the post button. Verify that the post appears on Chatter as expected.

By following these steps, you can successfully post on Chatter using Apex. This functionality can be useful for sharing updates, collaborating with users, and keeping everyone informed.

Sources:
ProntoForms Salesforce Integration Documentation
Webkul Blog – How to post to chatter feed from APEX
SalesforceCodex – Posting Rich Text Chatter using Apex

FAQs

What is Chatter in Salesforce?



Chatter is the social network feature in Salesforce that allows users to collaborate, share information, and communicate within their Salesforce org.

Can I post on Chatter using Apex?

Yes, you can post on Chatter using Apex. By leveraging Apex code, you can programmatically create and post feed items on Chatter.

How do I create an Apex class to post on Chatter?

To create an Apex class, start by defining a class with a method that handles the posting functionality. The method should use the FeedItem object to create a new feed item and set the necessary properties such as the parent ID and the body of the post.

What is the FeedItem object in Apex?

The FeedItem object in Apex represents a post or comment in Chatter. It contains properties such as the parent ID, which can be the ID of a user or record, and the body, which is the content of the post.

How do I insert a feed item in Apex?



After setting the properties of the feed item, you can use the `insert` statement to insert the feed item and create the post on Chatter.

How can I create a Visualforce page for posting on Chatter?

To create a Visualforce page, you can define a page with input fields for the user ID and the content of the post. Use a command button to trigger the posting action, which will call the method in the Apex class to create the feed item.

How do I test the functionality of posting on Chatter?



Once you have implemented the Apex class and Visualforce page, you can test the functionality by entering the required information in the input fields and clicking the post button. Verify that the post appears on Chatter as expected.

What are the benefits of posting on Chatter using Apex?

Posting on Chatter using Apex allows for automation and integration with other Salesforce processes. It enables you to programmatically share updates, collaborate with users, and keep everyone informed without manual intervention.