Triggers for Chatter Objects
You can write triggers for the FeedItem and FeedComment objects.
-
Only FeedItems of type
TextPost,QuestionPost,LinkPost,HasLink,ContentPost, andHasContentcan be inserted, and therefore invoke thebeforeorafter inserttrigger. User status updates don’t cause the FeedItem triggers to fire. -
While FeedPost objects were supported for API versions 18.0, 19.0, and 20.0, don’t use any insert or delete triggers saved against versions before 21.0.
-
For FeedItem, the following fields aren’t available in the
before inserttrigger:ContentSizeContentType
In addition, the
ContentDatafield isn’t available in any delete trigger. -
Triggers on FeedItem objects run before their attachment and capabilities information is saved, which means that
ConnectApi.FeedItem.attachmentinformation andConnectApi.FeedElement.capabilitiesinformation may not be available in the trigger.The attachment and capabilities information may not be available from these methods:
ConnectApi.ChatterFeeds.getFeedItem,ConnectApi.ChatterFeeds.getFeedElement,ConnectApi.ChatterFeeds.getFeedPoll,ConnectApi.ChatterFeeds.getFeedElementPoll,ConnectApi.ChatterFeeds.postFeedItem,ConnectApi.ChatterFeeds.postFeedElement,ConnectApi.ChatterFeeds.shareFeedItem,ConnectApi.ChatterFeeds.shareFeedElement,ConnectApi.ChatterFeeds.voteOnFeedPoll, andConnectApi.ChatterFeeds.voteOnFeedElementPoll -
FeedAttachment isn’t a triggerable object. You can access feed attachments in FeedItem update triggers through a SOQL query. For example:
-
When you insert a feed item with associated attachments, the FeedItem is inserted first, then the FeedAttachment records are created. 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, in Salesforce Classic FeedAttachment is available in
UpdateandAfterInserttriggers. When the attachment is done through Lightning Experience, it’s available in both theUpdateandAfterInserttriggers; but in theAfterInserttrigger, use the future method to access FeedAttachments. -
The following feed attachment operations cause the FeedItem update triggers to fire.
- A FeedAttachment is added to a FeedItem and causes the FeedItem type to change.
- A FeedAttachment is removed from a FeedItem and causes the FeedItem type to change.
-
FeedItem triggers aren’t fired when inserting or updating a FeedAttachment that doesn’t cause a change on the associated FeedItem.
-
You can’t insert, update, or delete FeedAttachments in before update and after update FeedItem triggers.
-
For FeedComment before insert and after insert triggers, the fields of a ContentVersion associated with the FeedComment (obtained through
FeedComment.RelatedRecordId) aren’t available.
- Apex code uses extra security when executing in a Chatter context. To post to a private group, the user running the code must be a member of that group. If the running user isn’t a member, you can set the
CreatedByIdfield to be a member of the group in the FeedItem record. - When CollaborationGroupMember is updated, CollaborationGroup is automatically updated as well to ensure that the member count is correct. As a result, when CollaborationGroupMember
updateordeletetriggers run, CollaborationGroupupdatetriggers run as well.
See Also
- Entity and Field Considerations in Triggers
- Object Reference for Salesforce and Lightning Platform: FeedItem
- Object Reference for Salesforce and Lightning Platform: FeedAttachment
- Object Reference for Salesforce and Lightning Platform: FeedComment
- Object Reference for Salesforce and Lightning Platform: CollaborationGroup
- Object Reference for Salesforce and Lightning Platform: CollaborationGroupMember