23 March 2015

Roll-up Summary Trigger (for Lookup Fields)

Many times over the course of my career I've bumped into this requirement, where a roll-up summary field is required on an object which does not have a master-detail relationship with another child object, rather a standard lookup field is used. As we know, the standard roll-up summary field only works with master-detail relationships, so in order to enable roll-ups for lookups we need to write some APEX.

Now this is not some break-through innovation or something that people are having trouble with, rather, it's a helper method that I've written to make my life easier by making it dynamic and have it here on my blog, because every single time this requirement comes up, I dig through my old projects to find the similar code that I've written some time ago, or sometimes I even write it from scratch. Let this serve you as a quick reference or a template which you can extend and add your extra bits and pieces like filter criteria etc.