Rollup summary fields are pretty common requirement in force.com customizations and app development. Rollups are easy to create on master-detail relationships as they are available as a field type. But on certain limits/situations we need to write apex code for rolling up the child information for common aggregations like COUNT, SUM, AVG, MAX/MIN etc, some of these limitations are
Only 10 rollup summary fields allowed per object on master detail relationships
Rollup child sobject records part of a lookup relationship. Native rollup summary fields are not available on LOOKUP relationships.
The ‘USUAL’ approach to handle this limitation is to either
Write trigger on various DML(reate, Update, Delete/Undelete) events on child sobject. These trigger either manually aggregated the information via Apex or used SOQL Aggregate queries for the same.
Write some batch/scheduled apex to perform this nightly.
I tried solving this problem by creating a small apex utility called “LREngine”. Complete description and details about it are available here on github: https://github.com/abhinavguptas/Salesforce-Lookup-Rollup-Summaries
Comments (2)
Anonymoussays:
October 12, 2012 at 11:00 amI have used this utility and I simply say its AWESOME and SUPERB.But I think there is a one bug in your utility.When there is single record to delete from child .After deletion parent Rollup fields won't update and value remain same (only in case if one child record is present).Can you please confirm whether my understaning or finding is correct and if correct then can you please tell me the solution for the same.
Anonymoussays:
October 12, 2012 at 3:53 pmThanks !!Can you please give steps and some code snippet, I will try this. In case you can debug the fix, please raise the pull request in github, i will merge it in the code base.