Salesforce API describes ConnectApi.ChatterFeeds.postFeedElement(communityId, subjectId, feedElementType, text) method which supposed to be used to create a feed item.

ConnectApi.FeedElement feedElement = ConnectApi.ChatterFeeds.postFeedElement(

System.debug('feedElement: ' + feedElement);

But I found that we can create a FeedItem directly by declaring a FeedItem object and inserting it.

FeedItem feedItem = new FeedItem();
feedItem.ParentId = getCollaborationGroup(EXAMPLE_FEED_GROUP_NAME).Id;
feedItem.LinkUrl = Url.getSalesforceBaseUrl().toExternalForm() + '/' + exampleCase.Id;
feedItem.Title = EXAMPLE_TITLE;
feedItem.Body = EXAMPLE_FEED_BODY;
insert feedItem;

What's the difference between these two approaches? Which one is preferred to use and why?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.