Newer Version Available
Triggers for Chatter Objects
You can write triggers for the FeedItem and FeedComment objects.
Trigger Considerations for FeedItem, FeedAttachment, and FeedComment
- Only FeedItems of type TextPost, LinkPost, HasLink, ContentPost, and HasContent can be inserted, and therefore invoke the before or after insert trigger. 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 are not available in the before insert trigger:
- ContentSize
- ContentType
In addition, the ContentData field is not available in any delete trigger.
-
Triggers on FeedItem objects run before their attachment and capabilities information is
saved, which means that ConnectApi.FeedItem.attachment information and ConnectApi.FeedElement.capabilities
information 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, and ConnectApi.ChatterFeeds.voteOnFeedElementPoll
- FeedAttachment is not a triggerable object. You can access feed attachments in FeedItem
update triggers through a SOQL query. For
example:
1trigger FeedItemTrigger on FeedItem (after update) { 2 3 List<FeedAttachment> attachments = [SELECT Id, Title, Type, FeedEntityId 4 FROM FeedAttachment 5 WHERE FeedEntityId IN :Trigger.new ]; 6 7 for (FeedAttachment attachment : attachments) { 8 System.debug(attachment.Type); 9 } 10} - 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.
- 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) are not available.
Other Chatter Trigger Considerations
- 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 CreatedById field 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 update or delete triggers run, CollaborationGroup update triggers run as well.