Create Roll-Up Summary Fields Using Salesforce Flow

Share this article...

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

10 thoughts on “Create Roll-Up Summary Fields Using Salesforce Flow

  1. Hello Team,

    In above scenario when a case record is deleted then it is not updating the count on Account object field. What could be the reason behind it ? The sub flow is executed before the record is deleted ?

      1. Hi Christine,

        This Flow is working for Bulk Insertion but not for Bulk Deletion. For Example, even if we delete 3 cases at a time using Data loader , the count is only reducing by 1. can you please check?

  2. Hi, I want to roll-up a contact’s opportunities in the last 36 months but I am being told “The options for setting the time frame of rolling up the opps is by: days back, years ago, or all time. There is no option to do it by months so I can’t put in 36 months.”.

    Is what our SF consultant writing to me correct?

    Based on what I have been reading I haven’t seen this limitation but I also don’t know what tool is being used.

  3. As per the salesforce best practices there should be one trigger per object. Then here we are crating multiple flows for one requiremnet. Can’t we use subflow in this case? also can’t we use decision element instead of defining criteria in first screen?

  4. Is it possible to create a flow in currency instead of a number?
    I want to show the total revenue amount of all opportunities of a account, on the account layout.

    But when I use a currency field, I don’t get the option Equals Count on the assigment.

  5. Do you know who I could do this if I have a units field on the property records and want to roll all these unit fields onto the Primary Contact record?

Add Comment