I wanted to post the code for the demos that we did during the webinar to help developers get familiar with the new features (and to prove that it wasn't smoke and mirrors!). I'll start with the Chatter trigger sample and will be blogging about other samples in the next couple of days. You can jump to this mark in the recording if you're only interested in the Chatter trigger demo and would rather not listen to the brilliant repartee between Dave and myself (tongue firmly in cheek).
To recap, Spring 11 introduces the ability to create Apex triggers on the FeedItem and FeedComment objects. Developers can now trigger real time business actions every time that someone posts to a User/Entity Feed or when someone comments on an existing post. The demo that I gave during the webinar addressed the following use case – "An AE can close an Opportunity (updating the Close Date and Stage and creating a reminder follow-up Task) by simply typing '!close' into the entity feed for the record". You can imagine why AEs would love such functionality if they were out in the field and wanted a quick way to close an Opportunity via their iXXX/Android/BB.
Here's the trigger code:
Lets walk through the code. First, notice that you write the trigger on a new 'generic' FeedItem object. You can't write triggers on the UserFeed or AccountFeed/OpportunityFeed etc. objects. That means that the trigger can fire as a result of several different actions – someone posting to a user's feed or someone posting to the entity feed of any object for which Chatter is enabled (Account, Opportunity, custom object etc.). Therefore, if your trigger code is only applicable to a specific user or entity feed (in this case, Opportunity), you need to filter for those posts in the trigger code. The best way to do so is to compare the prefix of the polymorphic 'parentId' field on the FeedItem object (which would map to the user/record for which the Chatter post was added) with the key prefix of the entity that you're interested in. That's exactly what lines 8 & 15 do.
Another point of note is the events that can trigger (no pun intended) a FeedItem or FeedComment Trigger. Since Chatter posts/comments can only be inserted or deleted (they cannot be updated or undeleted by a user), the triggers only support the 'insert' and 'delete' events.
A final note. FeedItems of type 'UserStatus' and 'TrackedChange' don't invoke the triggers. In other words, if a user updates his/her own status in Chatter or if a tracked field changes value, those events won't fire any triggers. Instead, you can write a trigger on the respective SObject if you want to act on a change to a field value and you can write a trigger on the User object for taking some action based on a user updating his/her Chatter status.
The rest of the trigger code is relatively straightforward and I won't delve into it. Happy coding and let me know if you have any questions about the trigger code.