Salesforce · Trigger

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. Rollup 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 Rollup 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.

 

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


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;
}

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

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

Advertisements

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

    Like

  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: https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigger_Best_Practices

    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! https://github.com/afawcett/declarative-lookup-rollup-summaries.

    I look forward to your future posts!

    Doug
    @DouglasCAyers

    Like

    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

      Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s