Monday, September 13, 2010

Deleting chatter feeds and comments

Recently I was working on some chatter stuff where i come across the requirement of deleting some chatter feeds (FeedPost) and comments(FeedComment). I try to delete these by making a direct query to the FeedPost and FeedComment object having the ids which i want to delete. But here comes the limitation.

Given post focus on deleting the feeds and comments, while taking these limitation in consideration.

Deleting FeedPost: FeedPost represents the following types of changes in a NewsFeed, UserProfileFeed, or record feed, such as AccountFeed: status updates, text posts, link posts, and content posts.

Limitation: One can't directly query for a FeedPost. FeedPosts are always associated with a feed item, so you can query for them through the feeds.

The following example returns all feed items for a given account, ordered by date descending:

SELECT Id, Type, FeedPost.Body FROM AccountFeed WHERE ParentId = AccountId ORDER BY CreatedDate DESC


So to delete FeedPosts we need to first query for it using its associated feed item and FeedPostsId as shown in the example below. Deleting the feed item will delete all the associated feeds.


List accountFeeds = [Select ParentId, Id, FeedPost.ParentId, FeedPostId From UserFeed where ParentId=: userid and FeedPostId=:chatterParentFeedId];
if(accountFeeds != null) {
delete accountFeeds;
}



Note: NewsFeed does not support delete call. It represents a Chatter feed on a user's home tab. A Chatter feed shows recent changes to records that the user is following.

Deleting FeedComment: A FeedComment is a child object of an associated UserProfileFeed, NewsFeed, or object feed, such as AccountFeed.

Limitation: One can't query FeedComment records directly. They can only be queried via the parent NewsFeed, UserProfileFeed, or entity feed, such as AccountFeed.

The following query returns information, including child FeedComment records:


SELECT ID, CreatedDate, CreatedById, CreatedBy.FirstName, CreatedBy.LastName, ParentId, Parent.Name,
(SELECT CommentBody, FeedItemId FROM FeedComments ORDER BY ID DESC)
FROM NewsFeed
ORDER BY CreatedDate DESC, ID DESC
LIMIT 20


So if we want to delete the comments associated with feed items, one need to query the feed item such as AccountFeed, NewsFeed etc along with FeedComments (filtered using comment id if available to get specific FeedComment record). The below example queries NewsFeed object along with the FeedComment object using the chatterNewsFeedId and chatterCommentId to get specific FeedComment object record to delete.


NewsFeed newFeed = [SELECT Id, (SELECT Id, CommentBody, CreatedDate, CreatedById, CreatedBy.FirstName, CreatedBy.LastName FROM FeedComments where id=:chatterCommentId) FROM NewsFeed where id=:chatterNewsFeedId];
if(newFeed != null) {
List comments = newFeed.FeedComments;
if(comments != null && comments.size() > 0) {
delete comments;
Apexpages.addMessage(new Apexpages.Message(Apexpages.severity.INFO, 'deleted successfully'));
}
}


Similarly one can query for the FeedComment objects using different feed items such AccountFeed, ContactFeed, UserProfileFeed etc. as per the requirement.