Newer Version Available

This content describes an older version of this product. View Latest

Working with Feeds and Feed Items

Feeds are made up of feed items. A feed item is a piece of information posted by a user (for example, a poll) or by an automated process (for example, when a tracked field is updated on a record). Because feeds and feed items are the core of Chatter, understanding them is crucial to developing applications with Chatter REST API and Chatter in Apex.

Salesforce Help refers to feed items as posts.

Note

How the Salesforce UI Displays Feed Items

To give customers a consistent view of feed items and to give developers an easy way to create UI, the Salesforce UI uses one layout to display every feed item, regardless of the feed item type. The layout always contains the same elements and the elements are always in the same position; only the content of the layout elements changes. If you stick to this structure, you won’t have to create a unique layout for every feed item type.

Feed item containing a file attachment and a comment.These are the feed item layout elements:
  1. Actor (ConnectApi.FeedItem.actor)—A photo or icon of the creator of the feed item. (You can override the creator at the feed item type level. For example, the dashboard snapshot feed item type shows the dashboard as the creator.)
  2. Preamble (ConnectApi.FeedItem.preamble)—Provides context. The same feed item can have a different preamble depending on who posted it and where. For example, Gordon posted this feed item to his profile. If he then shared it to a group, the preamble of the feed item in the group feed would be “Gordon Johnson (originally posted by Gordon Johnson)” and the “originally posted” text would link to the feed item on Gordon’s profile.
  3. Body (ConnectApi.FeedItem.body)—All feed items have a body, but the body can be null, which is the case when the user doesn’t provide text for the feed item. Because the body can be null you can’t use it as the default case for rendering text. Instead, use the text property of the feed item’s preamble, which always contains a value.
  4. Auxiliary Body (ConnectApi.FeedItem.attachment)—The visualization of the attachment. There are multiple attachment types. For example, for a link post, the attachment is the link and name, for a poll, it’s the poll data. In Chatter in Apex, the attachment types are subclasses of ConnectApi.FeedItemAttachment. In Chatter REST API, the attachment types are exposed as response bodies with the name Feed Item Attachment: Name, for example, Feed Item Attachment: Link and Feed Item Attachment: Poll. Make sure your code has a default case that doesn’t display an auxiliary body if it doesn’t recognize the attachment type.
  5. Created By Timestamp (ConnectApi.FeedItem.relativeCreatedDate)—The date and time when the feed item was posted. If the feed item is less then two days old, the date and time are formatted as a relative, localized string, for example, “17m ago” or “Yesterday”. Otherwise, the date and time are formatted as an absolute, localized string.

Here’s another example of a feed item in the Salesforce UI. This feed item’s auxiliary body contains a poll:Feed item containing text and a poll.

Feed Item Visibility

The feed items a user sees depend on how the administrator has configured feed tracking, sharing rules, and field-level security. For example, if a user doesn’t have access to a record, they don’t see updates for that record. If a user can see the parent of the feed item, the user can see the feed item. Typically, a user sees feed updates for:
  • Feed items that @mention the user if the user can access the feed item’s parent
  • Feed items that @mention groups the user is a member of
  • Record field changes on records whose parent is a record the user can see, including User, Group, and File records
  • Feed items posted to the user
  • Feed items posted to groups the user owns or is a member of
  • Feed items for standard and custom records, for example, tasks, events, leads, accounts, files, and so on

Feed Types

There are many types of feeds. Each feed type is an algorithm that defines a collection of feed items.

The algorithms, and therefore the collection of feed items, can change between releases.

Important

In Chatter REST API, the feed types are exposed in the resources. For example, these are the resources for the news feed and topics feed:
1/chatter/feeds/news/userId
2/chatter/feeds/topics/topicId
In Chatter in Apex, all feed types except Filter and Favorites are exposed in the ConnectApi.FeedType enum and passed to the ConnectApi.ChatterFeeds.getFeedItemsFromFeed method or to the ConnectApi.ChatterFeeds.postFeedItem method. This example gets the feed items from the logged-in user’s news feed and topics feed:
1ConnectApi.FeedItemPage newsFeedItemPage = 
2   ConnectApi.ChatterFeeds.getFeedItemsFromFeed(null, 
3     ConnectApi.FeedType.News, 'me');
4
5ConnectApi.FeedItemPage topicsFeedItemPage = 
6   ConnectApi.ChatterFeeds.getFeedItemsFromFeed(null, 
7     ConnectApi.FeedType.Topics, '0TOD00000000dUg');

To get a filter feed, call ConnectApi.ChatterFeeds.getFeedItemsFromFilterFeed. To get a favorites feed, call ConnectApi.ChatterFavorites.getFeedItems.

These are the feed types and their descriptions:
  • Bookmarks—Contains all feed items saved as bookmarks by the logged-in user.
  • Company—Contains all feed items except feed items of type TrackedChange. To see the feed item, the user must have sharing access to its parent.
  • Files—Contains all feed items that contain files posted by people or groups that the logged-in user follows.
  • Filter—Contains the news feed filtered to contain feed items whose parent is a specified object type.
  • Groups—Contains all feed items from all groups the logged-in user either owns or is a member of.
  • ModerationContains all feed items that have been flagged for moderation. The Communities Moderation feed is available only to users with “Moderate Community Feeds” permissions.
  • News—Contains all updates for people the logged-in user follows, groups the user is a member of, files and records the user is following, all updates for records whose parent is the logged-in user, and every feed item and comment that mentions the logged-in user or that mentions a group the logged-in user is a member of.
  • People—Contains all feed items posted by all people the logged-in user follows.
  • Record—Contains all feed items whose parent is a specified record, which could be a group, user, object, file, or any other standard or custom object. When the record is a group, the feed also contains feed items that mention the group.
  • To—Contains all feed items with mentions of the logged-in user, feed items the logged-in user commented on, and feed items created by the logged-in user that are commented on.
  • Topics—Contains all feed items that include the specified topic.
  • UserProfile—Contains feed items created when a user changes records that can be tracked in a feed, feed items whose parent is the user, and feed items that @mention the user. This feed is different than the news feed, which returns more feed items, including group updates.
  • Favorites—Contains favorites saved by the logged-in user. Favorites are feed searches, list views, and topics.
  • Filter—Contains the news feed filtered to contain items whose parent is a specified entity type.

Posting a Feed Item

Use these resources and methods to post feed items:
Feed Type Chatter REST API Resource Chatter in Apex Method
News Feed POST /chatter/feeds/news​/userId/feed-items

userId must be the ID of the logged-in user or the alias me.

ConnectApi.ChatterFeeds​.postFeedItem​(communityIdOrNull, ConnectApi.FeedType.News, userId)

userId must be the ID of the logged-in user or the alias me.

Record Feed POST /chatter/feeds/record​/recordId/feed-items ConnectApi.ChatterFeeds​.postFeedItem​(communityIdOrNull, ConnectApi.FeedType.Record, recordId)
User Profile Feed POST /chatter/feeds/user-profile​/userId/feed-items ConnectApi.ChatterFeeds​.postFeedItem​(communityIdOrNull, ConnectApi.FeedType.UserProfile, userId)

When you post a feed item, you’re creating a child of a standard or custom object. For Chatter REST API, specify the parent object in the userId or recordId section of the resource. For Chatter in Apex, specify the parent object in the in the userId or recordId argument.

The parent property of the posted feed item contains information about the parent object.

Select the correct feed type and parent object for the task you want to complete:
Post to yourself
Make a POST request to the news feed, the record feed, or the user profile feed.
For userId, specify the user ID of the logged-in user or the alias me.
The parent property of the newly posted feed item contains the User Summary object (ConnectApi.UserSummary) of the logged-in user.
Post to another user
Make a POST request to the record feed or the user profile feed.
For recordId or userId, specify the user ID of the target user.
The parent property of the newly posted feed item contains the User Summary object (ConnectApi.UserSummary) of the target user.
Post to a group
Make a POST request to the record feed.
For recordId, specify the group ID.
The parent property of the newly posted feed item contains the Group object (ConnectApi.ChatterGroupSummary) of the specified group.
Post to a record (such as a file or an account)
Make a POST request to the record feed.
For recordId, specify the record ID.
The parent property of the new feed item depends on the record type specified in recordId. If the record type is File, the parent is the File Summary object (ConnectApi.FileSummary). If the record type is Group, the parent is a Group object (ConnectApi.ChatterGroupSummary). If the record type is User, the parent is a User Summary object (ConnectApi.UserSummary). For all other record types, the parent is a Record Summary object (ConnectApi.RecordSummary).

Getting Feed Items from a Feed

Getting feed items from a feed is similar, but not identical, for each feed type.

To get the feed items from the company feed or the moderation feed, you don’t need to specify a subject ID:
Feed Type Chatter REST API Resource Chatter in Apex Method
Company GET /chatter/feeds/company/feed-items ConnectApi.ChatterFeeds​.getFeedItemsFromFeed​(communityIdOrNull, ConnectApi.FeedType.Company)
Moderation GET /connect/communities/communityId​/chatter/feeds/moderation/feed-items ConnectApi.ChatterFeeds​.getFeedItemsFromFeed​(communityIdOrNull, ConnectApi.FeedType.Moderation)
To get the feed items from the favorites and filter feeds you need to specify a favoriteId or a keyPrefix. The keyPrefix indicates the object type and is the first three characters of the object ID. For these feeds, the subjectId must be the ID of the logged-in user or the alias me.
Feed Type Chatter REST API Resource Chatter in Apex Method
Favorites GET /chatter/feeds/favorites​/subjectId/favoriteId/feed-items ConnectApi.ChatterFavorites​.getFeedItems(communityIdOrNull, subjectId, favoriteId)
Filter GET /chatter/feeds/filter​/subjectId/keyPrefix/feed-items ConnectApi.ChatterFeeds​.getFeedItemsFromFilterFeed​(communityIdOrNull, subjectId, keyPrefix)
To get the feed items from a record feed you need to specify a record ID.
Feed Type Chatter REST API Resource Chatter in Apex Method
Record GET /chatter/feeds/record/recordId/feed-items ConnectApi.ChatterFeeds​.getFeedItemsFromFeed​(communityIdOrNull, ConnectApi.FeedType.Record, recordId)

The recordId can be a record of any type that supports feeds, including group. The feed on the group page in the Salesforce UI is a record feed.

Tip

To get the feed items from all other feed types you need to specify a subject ID. Replace the feedType to specify a different feed. For all the feed types in this table except the user profile feed and the topics feed, the subjectId must be the ID of the logged-in user or the alias me.
Feed Type Chatter REST API Resource Chatter in Apex Method
Bookmarks, Files, Groups, News, People, To, Topics, User Profile GET /chatter/feeds/feedType/subjectId/feed-items

For example: GET /chatter/feeds/news/me/feed-items

ConnectApi.ChatterFeeds​.getFeedItemsFromFeed​(communityIdOrNull, feedType, subjectId)

For example: ConnectApi.ChatterFeeds​.getFeedItemsFromFeed​(communityIdOrNull, ConnectApi.FeedType.News, 'me')