0

I'm trying to get the attachments from a FeedItem after insert trigger, but it does not return all the associated attachments. I understand attachments are inserted after the FeedItem is inserted (ref.), but I am able to get the first attachment (and only the first attachment) in the after insert trigger. Is there any way to get all related attachments of a FeedItem in an after insert trigger?

On the high level - I am trying to convert regular posts to a specific CollaborationGroup to a Question, which I am handling with after insert trigger. While the post gets converted to Question, I am unable to pass its attachments. Any suggestions?

EDIT - Added code shippet.

Trigger:

trigger FeedItemTrigger on FeedItem (after insert) {
    List<Id> feedItemIds = new List<Id>();
    for(FeedItem fi : Trigger.New){
        feedItemIds.add(fi.Id);
    }
    List<FeedAttachment> attachments =  [SELECT Id, Title, Type, FeedEntityId FROM FeedAttachment WHERE FeedEntityId IN :feedItemIds];
    System.debug('attachments count :: '+attachments.size());
}

Chatter Post: Chatter Post

This gives gets me only one attachment from the FeedItem.

  • Can you add minimal required code by editing your question? Its hard to tell what is wrong in your code? – sanket kumar Jun 10 '19 at 7:19
  • @sanketkumar Thank you. I've edited the question. – yeselkay Jun 10 '19 at 9:27
0

The documentation you cite states that you need to use an after update trigger, not after insert

FeedAttachment is not a triggerable object. You can access feed attachments in FeedItem update triggers through a SOQL query. For example:

trigger FeedItemTrigger on FeedItem (after update) {

    List<FeedAttachment> attachments =  [SELECT Id, Title, Type, FeedEntityId 
                                         FROM FeedAttachment 
                                         WHERE FeedEntityId IN :Trigger.new ];

    for (FeedAttachment attachment : attachments) {
        System.debug(attachment.Type);
    }
}

When a feed item with associated attachments is inserted, the FeedItem is inserted first, then the FeedAttachment records are created next. On update of a feed item with associated attachments, the FeedAttachment records are inserted first, then the FeedItem is updated. As a result of this sequence of operations, FeedAttachments are available in update triggers only, and aren’t available in insert triggers.

  • As mentioned in my question, I'm converting a Post to Question (which works well), for which I have to use a trigger on insert event and not update. I am looking for suggestions to obtain related attachments in after insert trigger. – yeselkay Jun 10 '19 at 15:17
  • @yeselkay the doc says you can't do this, you need to use an after update trigger on FeedItem. This after update trigger will fire automatically after the attachment(s) are inserted and you should adjust your logic to deal with related object creation accordingly – cropredy Jun 10 '19 at 15:40

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.