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(
Network.getNetworkId(),
getCollaborationGroup(EXAMPLE_FEED_GROUP_NAME).Id,
ConnectApi.FeedElementType.FeedItem,
EXAMPLE_FEED_BODY
);
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?