Admins / Developers / Flow

Create Roll-Up Summary Fields Using Salesforce Flow

By Andrew Cook

Roll-up summary fields are great! But it’s important to remember that they are only available out-of-the-box between objects with a Master-Detail relationship. 

Never fear, as in this article, we’ll be learning how to create roll-up fields between objects that have a Lookup relationship using the mighty Flow Builder. This includes a simple use case as well as a quick walkthrough of Flow-friendly roll-up features to help you get started.

What Is a Roll-Up Summary Field?

A roll-up summary field is one that aggregates data from a child object to a parent object that shares a Master-Detail relationship. Roll-up summary fields can use the COUNT, SUM, MIN, and MAX functions. For example, we could use a roll-up summary field to display the total value (amount) from opportunities on a related account.

Even better, roll-up summary fields can have filter criteria, so we could choose to display the total opportunity value from won opportunities on a related account. 

Unfortunately, roll-up summary fields are only available for objects in a Master-Detail relationship, and are not available for those that have a Lookup relationship. 

Create Roll-Up Summary Fields with Flow

Historically, admins have turned to external tools to achieve roll-up summary fields between objects that have a Lookup relationship. These tools include Declarative Lookup Rollup Summaries, better known as DLRS (which I absolutely adore!), and Rollup Helper.

READ MORE: How to Create Roll-Up Summary Fields in Salesforce with Rollup Helper

However, it is also possible to achieve this functionality using Flow. Flow can implement many types of roll-up logic: Count, Summary, Min, Max, Average, First, and Last. Logic can range from simple to complicated. Here are a few common examples of aggregation:

  • How many contacts does an account have?
  • How many contacts of a specific role does the opportunity contain?
  • How many team members does an account have?
  • How many records of a custom object are related to an opportunity?

Using a Scheduled Flow

There is a simple reason for using a scheduled flow over a record-triggered flow. That reason is deleted records. Currently, Flow only gives the option of a before-delete trigger on flows. But for a record-triggered flow to work sufficiently, we would need an after-delete trigger to launch the flow.

Because of this, a scheduled flow is the more logical trigger. This means the flow will run at a specified time and interval of your choosing, count all of the records at that time, and then update the roll-up field accordingly. Of all the ways I have tried to do this, a scheduled flow seems to work best (for the time being). 

One thing to bear in mind here is that scheduled flows run with the automated process user. This is useful to know in case you have any additional workflows that are firing based on the last updated user for a record.

Use Case Walkthrough: Assets Related to Accounts

This is something I’ve experienced in the past. An external system creates Asset records in Salesforce, which is a scheduled job that runs every morning in the early hours. Once these records have been created, there’s nothing on the account record to indicate the total number of Asset records. This is a slimmed down version of that process.

So, we want to know the Count of Assets related to an Account.

Dedicated Field on the Account

The first step in this process is a simple one. You need to create a number field on the account object, which will then be populated by the flow. This field needs to have zero decimal places, and is recommended that users don’t have access to edit it to ensure data integrity.

Creating the Flow

As stated above, for this we want to use a scheduled-triggered flow.

Next we need to set the schedule criteria for the flow, including a start date, run time, and the frequency in which you want the flow to run. My flow was set to run in the morning after the other integration had run which created the Asset records.

Now we need to set the object this flow needs to run on, and any conditional requirements. I’m running this on all account records, but you could include things like record types or regions in this criteria if applicable, depending on the subset of records that should go through this automation.

We then need to add a Get Record element to the flow. This will be used to get all of the associated Asset records of the account. We do this by adding the conditional requirement of the Account ID of the Asset record being {!$Record.Id}.

We also need to store all records in this element, which automatically creates a collection variable for us to use in the flow.

Next, we need to add a loop element to loop through the asset records. Here we can use the collection variable from the get records element previously.

READ MORE: Salesforce Flow Loops – Best Practices and Examples

We now need to create a number variable, the purpose of which is to hold the number count of Asset records. This variable needs to have 0 decimal places.

Now, using the number variable above, we use an assignment element. This element is assigning the number of assets related to the account to the number variable.

Lastly, we need to add in an Update Records element to the flow after the flow loop. In this element we need to set the Total Assets field on the acount record to the AssetCount number variable we have set in the flow.

Your final flow should now look something like this:

Now you just have to save and activate your flow.

You can now add your field to the page layout, compact layout, and so on.

For the latest documentation regarding scheduled flow, please refer to the Salesforce Help here.

Summary

Now that you are familiar with the mechanics and main services of roll-up summary field creation (with the powers of Flow Builder!), you can apply these principles to more creative and complex use cases, e.g. Count Distinct, Concatenate, and many others. Good luck! 

The Author

Andrew Cook

Andrew is a Salesforce Technical Instructor at Salesforce Ben. He is 14x certified and has worked in the ecosystem for 12 years.

Leave a Reply