Trying to find a way to link FeedItem records to the CaseComment they are related to. Have a Lightning Component pulling in Case information, but failing to find a reliable way to relate the replies to CaseComments which are listed as FeedComments under FeedItems. Is there a way to reliably associate the FeedItems back to the CaseComment?
1 Answer
Found out how to go about it. There's no link between the CaseComment records and the FeedItem records that can be accessed via the usual SOQL route. What you can do instead is make use of the Connect in Apex functionality which makes calls via the ConnectApi.
The ChatterFeeds sub-class has methods to call out to the API which returns both FeedItems (and in this case more specifically their parent FeedElement object) and it's associated CaseComment data with a bit of related data digging. You can access the CaseComment data via the FeedElement.capabilities field which gives you access to FeedElementCapabilities class. This then gives you access to the FeedElementCapabilities.caseComment field (if the FeedElement has that capability, as in it's querying a CaseComment) which returns the CaseCommentCapability class which then has the CaseComment data.
Overall structure is something like:
- ConnectApi
- ChatterFeeds (Use method to get collection of FeedElement)
- FeedElement
- FeedElementCapabilities
- CaseCommentCapability (desired CaseComment data here)
- FeedElementCapabilities
- FeedElement
- ChatterFeeds (Use method to get collection of FeedElement)
For my usage, I did a SOQL query for the limited set of FeedItem records I wanted from a Case and then used that to build an Id collection for retrieving from the ConnectApi via the ChatterFeeds.getFeedElementBatch method.
//Get relevant FeedItem data, used this since I have additional LIMIT and OFFSET requirements plus additional sub-query data I wanted
List<FeedItem> feedItemList = [
SELECT Id, Type, CreatedBy.Name, CreatedDate,
(SELECT FeedItemId, CommentBody, CommentType, RelatedRecordId, CreatedDate, CreatedBy.Name FROM FeedComments ORDER BY CreatedDate DESC)
FROM FeedItem
WHERE ParentId = :caseId
AND Type = 'CaseCommentPost'
ORDER BY LastModifiedDate DESC
LIMIT :limitValue
OFFSET :offsetValue
];
//Build collection of Ids
List<String> feedItemIdList = new List<String>();
for(FeedItem fi : feedItemList) {
feedItemIdList.add(fi.Id);
}
//Get ConnectApi data
Map<Id, ConnectApi.FeedElement> feedElementMap = new Map<Id, ConnectApi.FeedElement>();
List<ConnectApi.BatchResult> batchResultList = ConnectApi.ChatterFeeds.getFeedElementBatch(Network.getNetworkId(), feedItemIdList);
for(ConnectApi.BatchResult batchResult : batchResultList) {
if(batchResult.isSuccess()) {
if(batchResult.getResult() instanceof ConnectApi.FeedElement) {
ConnectApi.FeedElement feedElement = (ConnectApi.FeedElement) batchResult.getResult();
feedElementMap.put(feedElement.Id, feedElement); //Will have the same Id as the FeedItem record query
}
} else {
System.debug(batchResult.getErrorMessage());
}
}
//Use the data
for(FeedItem feedItem : feedItemList) {
ConnectApi.CaseCommentCapability caseCommentCapability = feedElementMap.get(feedItem.Id)?.capabilities?.caseComment;
//Might want to throw the desired data into a custom Wrapper you define to make working with the data easier from that point
CaseCommentWrapper wrapper = new CaseCommentWrapper(feedItem, caseCommentCapability);
}
-
nice; sadly one needs to use SeeAllData=true to unit test ConnectApi, sigh– cropredyCommented Aug 19, 2022 at 17:03
-
@cropredy Yep, testing has ended up being rather annoying with this (especially since getFeedElementBatch isn't one of the supported setTest methods), but at least it's doable with the right setup. Commented Aug 19, 2022 at 17:55