ABSYZ ABSYZ

  • Home

    Home

  • About us

    Who We Are

  • Our Expertise

    What we Do

  • Our Approach

    How We Do It

  • Products

    What We Made

  • Industries

    Who We Do It For

  • Clients

    Whom We Did It For.

  • Article & Blogs

    What Experts Think

  • Careers

    Join The Team

  • Get In Touch

    Let’s Get Started

ABSYZ

Roll-up Summary without a Master Detail Relationship

Home / Article & Blogs / Salesforce / Roll-up Summary without a Master Detail Relationship
By Haseeb Khan inSalesforce, Trigger

roll-up summary

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

LookupMaster-DetailRoll Up
119
Like this post
4 Posts
Haseeb Khan

Search Posts

Archives

Categories

Recent posts

BioAsia 2023 in Hyderabad: An Annual International Event

BioAsia 2023 in Hyderabad: An Annual International Event

The Role Of Marketing in Small & Medium Enterprises

The Role Of Marketing in Small & Medium Enterprises

Salesforce For Retail: How Salesforce CRM Can Help Retailers

Salesforce For Retail: How Salesforce CRM Can Help Retailers

What is ChatGPT & How Does It Work?

What is ChatGPT & How Does It Work?

What Is Graphic Design? (Executive Summary 2023)

What Is Graphic Design? (Executive Summary 2023)

  • Previous PostConnect Salesforce with Microsoft office
  • Next PostEmail Services in Salesforce with a simple example
    Connect Salesforce with Microsoft office

Related Posts

Salesforce For Retail: How Salesforce CRM Can Help Retailers
Salesforce

Salesforce For Retail: How Salesforce CRM Can Help Retailers

Introduction To Copado Devops Tool
Salesforce

Introduction To Copado Devops Tool

What is Salesforce Code Builder?
Salesforce

What is Salesforce Code Builder?

Automation in Healthcare And Its Benefits
Health Cloud Salesforce

Automation in Healthcare And Its Benefits

5 Comments

  1. Charles
    Reply
    17 March 2016

    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

    Reply
    • Nitish
      Reply
      28 March 2016

      Hi Charles. Yes the events will be fired on after update events.

      Reply
    • Apoorva Mathur
      Reply
      9 May 2017

      Hi,
      Thanks for this blog.. Helped me a lot!!

      Reply
  2. Doug Ayers
    Reply
    24 March 2016

    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

    Reply
    • Nitish
      Reply
      28 March 2016

      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

      Reply

Leave a Reply (Cancel reply)

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

*
*

ABSYZ Logo

INDIA | USA | UAE

  • About us
  • Article & Blogs
  • Careers
  • Get In Touch
  • Our Expertise
  • Our Approach
  • Products
  • Industries
  • Clients
  • White Papers

Copyright ©2022 Absyz Inc. All Rights Reserved.

youngsoft
Copy
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “ACCEPT ALL”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent. Privacy Policy
Cookie SettingsREJECT ALLACCEPT ALL
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled

Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.

CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.

Functional

Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.

Performance

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

Analytics

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.

Advertisement

Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.

Others

Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.

SAVE & ACCEPT