I've got a requirement to mirror a Case's Chatter feed into another object (for safe-keeping, as it were). My first idea was to try a trigger on the FeedItem object, catch it and clone it, and just switch the ParentId to the other object's Id.

    for(FeedItem originalItem: feedItemsMap.get(selectedCase.Id)){
        FeedItem repostedItem = originalItem.clone(false, true, true, true);
        repostedItem.ParentId = newObjectId;
    insert repostedItems;

But apparently this only works for a small subset of possible actions (i.e. TextPost and Comments), and we have a lot of possible feed items like Activities and Emails sent and received.

(Also, I did look into this answer but it only covers the basic use cases)

So, after many many hours I turned to the ConnectApi methods and thought, easy enough that I could make a Batch Apex that reads the entire case's feed and just reposts it 1-to-1

List<ConnectApi.FeedElement> allFeedItems = ConnectApi.ChatterFeeds.getFeedElementsFromFeed(null, ConnectApi.FeedType.Record, currentCase.Id).elements;

for(ConnectApi.FeedElement feedItem: allFeedItems){
   //container for the reposted element
   ConnectApi.FeedItemInput input = new ConnectApi.FeedItemInput();

   //set subjectid to new related object id
   input.subjectId = newObjectId;

   ConnectApi.BatchInput batchInput = new ConnectApi.BatchInput(input);

ConnectApi.ChatterFeeds.postFeedElementBatch(Network.getNetworkId(), batchInputs);

But the ConnectApi is super confusing and this didn't seem to work, so I tried making a Share Instead:

ConnectApi.FeedEntityShareCapabilityInput shareInput = new ConnectApi.FeedEntityShareCapabilityInput();
shareInput.feedEntityId = feedItem.Id;

ConnectApi.FeedElementCapabilitiesInput feedElementCapabilitiesInput = new ConnectApi.FeedElementCapabilitiesInput();
feedElementCapabilitiesInput.feedEntityShare = shareInput;

input.capabilities = feedItem.capabilities;

This also didn't work, I got a "INVALID_CROSS_REFERENCE_KEY]: Operation only allowed on published feed entity on public groups or user profiles" error.

So at this point I'm stuck, any ideas on how I could make this requirement possible?

Thanks in advance!

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.