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 Salesforce.com 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.

COUNT :

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>();
if(trigger.isInsert){
for(Desk__c deskItem : Trigger.new){
classroomIDs.add(deskItem.Classroom__c);
}
}
else if(trigger.isDelete){
for(Desk__c deskItem : Trigger.old){
classroomIDs.add(deskItem.Classroom__c);
}
}
else if(trigger.isUnDelete){
for(Desk__c deskItem : Trigger.new){
classroomIDs.add(deskItem.Classroom__c);
}
}
else if(trigger.isUpdate){
for(Desk__c deskitem : trigger.new){
if(trigger.oldmap.get(deskitem.id).clasroom__c!=deskitem.classroom__c){
classroomIDs.add(deskItem.Classroom__c);
classroomIDs.add(trigger.oldmap.get(deskitem.id).classroom__c);
}
}
}
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;
ct.add(cust1);
}
update ct;
}
[/sourcecode]

——————————————————————

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

future-of-ai-powered-business-solutions
Dreamforce 2024: Unveiling the Future of AI-Powered Business Solutions
gitex 2024 worlds largest tech show
GITEX 2024 |  World's Largest Tech Show - ABSYZ
inclusive practices in women leadership equitable future
Inclusive Practices in Women’s Leadership: Equitable Future
salesforce dreamfest 2024 the highlights of dreamforce 2024
Dreamfest 2024: The highlights of Dreamforce 2024
DocuSign CLM the ultimate tool for managing contracts
DocuSign CLM: The Ultimate Tool for Managing Contracts
Document

How can i help you? close button

powered     by   ABSYZ
Scroll to Top