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!!

Leave a Comment

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

Recent Posts

how can salesforce experience cloud drive customer engagement rates
How Can Salesforce Experience Cloud Drive Customer Engagement Rates?
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
Scroll to Top