Roll-up Summary without a Master Detail Relationship

Roll-up Summary without a Master Detail Relationship

Roll-up Summary is a feature of salesforce enabled only after having a master detail relationship between objects. Roll-up Summary is a feature where, It gives us the options of having SUM, MIN, MAX of a field in all the child records or COUNT the number or child records. This is stored as a field in the Master side Object of the two objects in a master detail relationship.

As has limitations on almost everything available we have limits for Roll-up Summaries as well!! We can configure a maximum of 25 Rollup Summaries  .The maximum number of master detail relationships on an object can be two.

To cater such rollup requirements we need to have a master detail relationship between objects . But we cannot just go ahead configuring a master detail relationship on a requirement just for the feature ROLLUP. There are several other features of master detail relationship to be kept in mind,

– The relationship field is required on all detail records.
– The ownership and sharing of a detail record are determined by the master record.
– When a user deletes the master record, all detail records are deleted.

Moreover, A standard object cannot be the child/detail in a Master-Detail relationship. An example is that you cannot create a Master-Detail relationship from the Asset object because the Asset cannot be the child in a Master-Detail relationship.

So, I came up with a trigger that can meet the functionalities available in a Rollup without having a Master Detail. All you need is a lookup relationship between the objects.


So let’s say you have a classroom object and a desk object. Where we have to count the number of desks in a classroom . We write a trigger after having a look up between them.

We basically get all the parent record with the count__c field and load them in a map. This works fine for insert, update and delete.

-When a child record is inserted, the count in the map is increased for the specified parent.

-When a child record is updated, three scenarios can happen:

  1. The lookup field is changed from one parent to another
  2. There is no value in the lookup field and is updated to a value
  3.  The lookup value is nullified basically the parent is removed.

To meet all scenarios a code is written that works accordingly.

-When a child is deleted, the count is reduced in the map for the respective parent.

In the end the values in the map are updated to the database.


[sourcecode language=”java”]

trigger deskMaster on Desk__c (after delete,after update, after insert, after undelete) {
list<classroom__c> ct = new list<classroom__c>();
set<id> classroomIDs = new set<id>();
for(Desk__c deskItem :{
else if(trigger.isDelete){
for(Desk__c deskItem : Trigger.old){
else if(trigger.isUnDelete){
for(Desk__c deskItem :{
else if(trigger.isUpdate){
for(Desk__c deskitem :{
AggregateResult[] groupedResults = [SELECT COUNT(Id), classroom__c FROM desk__c where classroom__C IN :classroomIDs GROUP BY classroom__c ];
for(AggregateResult ar:groupedResults) {
Id custid = (ID)ar.get(‘classroom__c’);
Integer count = (INTEGER)ar.get(‘expr0’);
classroom__c cust1 = new classroom__c(Id=custid);
cust1.count__c = count;
update ct;


This can be edited according for the SUM, MIN, MAX functionalities. The structure will remain the same, you just have to change the logic inside.

Happy Coding!!

5 thoughts on “Roll-up Summary without a Master Detail Relationship”

  1. Hi Haseeb,

    For Min, Max and Sum we need to include the after update event which fires when the field value is modified.

    -Charles M

  2. Hi Haseeb,

    First time reader here, saw a link on Twitter. Great post and thanks for sharing! I like your careful thoughtfulnes on pros/cons of introducing a master-detail field as that isn’t always the answer due to sharing access implications.

    Not sure if you use the Trigger Handler design pattern. If not, I encourage you to check out this article to help manage and structure your triggers. You have clean code so I think you’ll get the idea pretty quick:

    For future needs, if you didn’t want to get into the hassle of writing an apex trigger and unit test, check out Andy Fawcett’s free tool “Declarative Lookup Rollup Summaries” app. It provides a point-n-click declarative interface and it manages the trigger/tests for you!

    I look forward to your future posts!


    1. Thanks @DouglasCAyers for your valuable comments. The reason for putting everything in the trigger so that everything is in one place. But definately using Helper functions is a way to go

      The link shared by you is really helpful. Thanks Again

Leave a Comment

Your email address will not be published. Required fields are marked *

Recent Posts

salesforce world tour essentials mumbai
Salesforce World Tour Essentials Mumbai (19 June, 2024)
benefits of using salesforce for higher education Industry
Benefits Of Using Salesforce For Higher Education Industry?
salesforce for real estate
Salesforce for Real Estate: How Salesforce CRM Can Help Realtors
salesforce consumer goods cloud store setup
A Detailed Look into Salesforce Consumer Goods Cloud Store Setup
salesforce revenue cloud does your business need it
Salesforce Revenue Cloud: Does Your Business Need It?
Scroll to Top