How to create rollup summaries in a declarative way in Salesforce

By

7 minutes de lecture
DLRS : How to create rollup summaries in a declarative way

How often did you need to use information from parent record on child record ? Maybe you also reminded or concatenated some fields on child record, right ?

Standard functionalities of Salesforce allow to do it easily. You can create standard fields like formula or rollup summary fields and that’s it ! Even if the formula could be arduous, you could succeed with no coding.

However, did you face some impossible cases if you wasn’t developper ? I had.

Indeed, with no coding, how do you jump from child record to a parent record or worth if tables haven’t a master-detail relationship … ?

Maybe you just want to display a field from child record to the parent record with or without filter.

Or you require to show the full name of the person who completed the last activity on the opportunity record.

Even you need to create a rollup summary field with a rollup summary field as source field.

So I needed help like apex and developer work … until I discover the amazing Andrew Fawcett’s tool called Declarative Lookup Rollup Summaries (DLRS) !

How to install DLRS app ?

First of all, don’t search or install DLRS from the appexchange. It would be a very outdated version. Instead of that, go on the github link to install the package in a Sandbox or a Production environment :

https://github.com/afawcett/declarative-lookup-rollup-summaries.

It would be better to test in a Sandbox first.

Is everything fine ?

Once the app installed, you can go on the Declarative Lookup Rollup Summaries App and go to the Welcome tab. You should see an error message : don’t panic, everything is normal !

The tool explains you that it cannot talk to SFDC API.

Click on “Create Remote Site Setting” button to fix the error.

Click on “Create Remote Site Setting” button to fix the error

How to use DLRS?

Now that’s everything is really fine, you can go on Manage Lookup Rollup Summaries tab to create a new Lookup Rollup Summary.

You find below some details on each field in order to help you during the creation.

  • Lookup Rollup Summary Name — Label.
  • Lookup Rollup Summary Unique Name — API name: unique, no special character, no space.
  • Parent Object — API name of the parent object: don’t forget ‘__c’ in case of custom object. Think of the Parent Object as the object you want the data to be written to.
  • Child Object — API name of the child object: don’t forget ‘__c’ in case of custom object. Think of the Child Object as the object you want to get the data from.
  • Relationship Field — API name of the field who links child object to the parent object: it could be a text field, a normal lookup field or a master lookup field. One more time, don’t forget ‘__c’ in case of custom object.
  • Relationship Criteria — SOQL query with wanted criteria: matches WHERE clause of a SOQL query. You can use AND statements and OR statements inside parentheses.
  • Relationship Criteria Fields — API names list of used fields in Relationship Criteria; one field by line.
  • Field to Aggregate — API name of the field on the child object, field to aggregate.
  • Field to Order By — useful only for some operations: Concatenate, Concatenate Distinct, Last and First. This field allows to sort records by specific field and not by the Field to Aggregate, by default. You could order results by CreatedDate or LastModifiedDate for example.
  • Aggregate Operation — Sum, Max, Min, Avg, Count, Count Distinct, Concatenate, Concatenate Distinct, First, Last.
  • Aggregate Result Field — API name of the field on the parent object, field to store the result.
  • Aggregate All Rows — if the checkbox is true, records in the Recycle Bin or archived by the system would be included in the calculation.
  • Row Limit — useful only for some operations: Concatenate and Last. This field allows to define a set row count.
  • Active — this checkbox activates and deactivates the Lookup Rollup Summary.
  • Calculation Mode:
  • Realtime — as soon as and each time a user makes a change, this mode would trigger DLRS, calculate rollup and update the result on the parent object. You must deploy the child apex trigger.
  • Scheduled — this mode schedules apex to run whenever you plan. On whatever cadence you want the apex class RollupJob runs and collects all Lookup Rollup Summary Schedule Items in order to calculate rollups and update the result on the parent object. You must deploy the child apex trigger. If you need to check if this mode works, check Setup > Jobs > Scheduled Jobs & Apex Jobs.
  • Developer — this mode allows to call DLRS from your own developments.
  • Process Builder — this mode allows to call DLRS from inside Process Builder. No need to deploy the child apex trigger.
  • Calculation Sharing Mode:
  • User — this mode respects system sharing rules and calculates the rollup based on user who triggers the DLRS visibility.
  • System — this mode accesses on entire system as apex and workflow rules and calculates the rollup bases on all child records.
  • Concatenate Delimiter — character who splits the result of the concatenation: , or | or a line break BR().
  • Description — rollup description, note for yourself or other admins.
  • Test Code — custom test code

If some fields are not standard fields, they have to be created before created the Lookup Rollup Summary: Relationship Field, Relationship Criteria Fields, Field to Aggregate, Field to Order By, Aggregate Result Field.

Just few more steps

If you choose a calculation mode where you need to deploy the child apex trigger, save your DLRS first without activate it.

After saving your DLRS, you would see some new buttons.

  • Delete — I hope you understand what this button is for :)
  • Manage Child Trigger — this button allows you to deploy the child apex trigger and to remove it. You only need to deploy the trigger one time per child object that you have DLRS running on: if you have 3 DLRS records all running on Opportunity as the child object you need only deploy the trigger one time.
  • Calculate — this button allows to run the calculation: great for a new DLRS. Think to activate the DLRS before clicking on Calculate button. On the Run Calculate Job page you could see a text box to apply some parent object filters if needed.
  • Schedule Calculate — this button allow to schedule the calculation of the DLRS. Think to activate the DLRS before clicking on Schedule Calculate button. On the Schedule Recurring Calculate Job page you could see a text box to apply some parent object filters if needed. Moreover, you chose in the same page the frequency and the time for running the apex trigger.

What is awesome about DLRS ?

Not only DLRS has 10 aggregate operations and 4 calculation modes but it also Lightning ready and quite easy to use for Salesforce Admins.

Cherry on the cake, DLRS stored rollups as Custom Metadata: allowing your rollups to be added to Change Sets and Packages and are also automatically cloned during Sandbox refresh.

Don’t forget this app is free !

Additional information

Last but not least, you could find all information on github. This blog gives: step by step of how to DLRS, the most common issues you might run into with it, few ways to use it, how to assign permission to DLRS, how to add a new field to the Rollup Definition, etc.

Need help? Trailblaizer Community Group!

Read more posts

Enforce code standards with PMD

Developers working on a project usually set coding rules to have a standardized codebase. It is an important piece of the code maintainability, and it can be very easy …
March 2023
Advices
Scratch orgs

Uncovering Salesforce Settings: A Step-by-Step Guide for Scratch Orgs

Today, it’s pretty easy to build your Scratch Org definition file when you know what Settings you want to activate, as they are mapped with the same setting names …
February 2023
Advices
Business Analyst

Core qualities of a Business Analyst?

A common definition we are used to hear is that being a Business Analyst means to have a combination of both hard skills and soft skills. What does a …
June 2022
Advices
Image d'illustration d'une employée travaillant sur un ordinateur portable

Process builder and workflow make way to Flows (½)

Overview “If you can do it with a Workflow, then do it with a Process Builder, because everything a Workflow does, a Process Builder does it better”. If you …
March 2022
Advices

Day 22 : Salesforce new “Migrate To Flow tool” in Spring 22

As most of you already know, the workflow rules and process builders are planned to be retired in 2023 (no precise date defined so far). Today, I’m going to …
December 2021
Advices

Day 18 : Fake callout responses for test classes !

Hello everybody ! Today let’s talk about Apex tests classes in Salesforce. Everyone loves a good test class, and Salesforce makes it official by requiring to have a minimum …
December 2021
Advices