From James Simone: Create Powerful Rollups in your Flows with a Single ‘Perform Rollup’ Action

Author: James Simone

A common question we see all the time when looking to roll data up is – “how do I easily roll up data when it’s not a Master-Detail relationship?” Previously, there wasn’t a good, free, flow-friendly and scalable method for doing so. The aspiring Flow acolyte might be tempted to think: “I could do this in flow!” After all, you’re just a few looping elements and assignment nodes away from having a rudimentary calculator figured out – and you did it yourself! Brilliant.

Then the requests start pouring in. Management loved that rollup you built so much that they’re asking for fifteen more fields to be rolled up to the parent-level too. They’d also like a dozen other metrics tallied in different objects. Now, you’re stuck with the unenviable task of trying to implement rollup features like concatenation, sums, counts, and more across a broad swath of objects. Suddenly, the prospect of wiring everything up in Flow seems a bit more daunting …

Introducing Apex Rollup

It doesn’t have to be so hard. Flow’s Apex Actions represent the next-level of customization when it comes to flexibility, speed, and ease of adoption, and this installable Apex Rollup extension takes care of all the necessary legwork when it comes to configuring rollup summary fields across any standard or custom Salesforce object with a lookup-based relationship – it even works with non lookup-based fields, as long as there’s a Text field that matches between objects!

Here’s a video introduction

Installation

Plus, because it’s an unmanaged package, it’s easy to install and upgrade to both sandboxes and production Salesforce orgs. Simply click the appropriate button on the Github Readme in order to get started:

Once you’ve installed the package, it’s on to our first exhibit! Let’s take a look at a common pain point in Sales Cloud – summing the Total Price field from Opportunity Products to their parent Opportunity’s Amount field.

Overview

This package installs 2 new Flow Actions called Perform Rollup and Perform Deferred Rollup that you can add to flows that, for example, trigger on changes to an Opportunity Product record. The action reads configuration information from a Custom Setting and one or more Custom Metadata Type Records and you can make changes to these records to add calculations to the flows that use this action without having to change those flows. 

How To Easily Sum Opportunity Product Prices To Opportunity Amount

Apex Rollup will make use of both a Custom Setting and a Custom Metadata Type to store configuration settings.

First things first – Apex Rollup ships with a Custom Setting, Rollup Settings, whose Default Organization Level values need to be enabled.

Configuring the Rollup Settings Custom Setting (One Time Step)

  1. In Setup, navigate to Custom Settings
  2. Click Manage next to “Rollup Settings”
  3. Click the Edit button above the “Default Organization Level Value” heading
  4. Check the “Is Enabled” flag

Creating Rollup Custom Metadata Type Records

Secondly, we need to configure our rollup’s objects and relationships. Apex Rollup stores this configuration information in a Custom Metadata Type (CMDT) called ‘Rollup’ that gets installed with the project. 

  1. Go to Setup –> Custom Metadata Types
  2. Click the Manage Records button next to the Rollup custom metadata type
  3. Click the New button
  4. Set up your custom metadata record so that the Parent Object values are correctly configured for Opportunity, and the Child Object values are correctly configured for Opportunity Product
    1. The Label field can be whatever you’d like. We tend to organize our rollup labels using the convention “Operation Child Object Child Field to Parent Object Parent Field” – which, for our example, comes out to “Sum OLI Price to Opp Amount” for the label
    2. The Parent Object should be Opportunity
    3. The Lookup Field On Lookup Object should be Opportunity Id
    4. The Rollup Field On Lookup Object should be Amount
    5. The Child Object’s Calc Item should be Opportunity Product
    6. The Lookup Field On Calc Item should be Opportunity Id
    7. The Rollup Field On Calc Item should be Total Price
    8. The Rollup Operation should be set to SUM
    9. The Rollup Control lookup record should be set to the Org_Defaults record that comes pre-installed with Rollup. More on this, later.
    10. Click Save – congrats, you’ve done it!

And here’s what the fully configured rollup CMDT record should look like:

Setting Up The Opportunity Product Record-Triggered Flow

Now that our settings are configured, we’re on to create a flow that makes use of our Rollup action.  We’ll walk through the creation of a new flow, but if you already have a RT flow set up for Opportunity Product, you should be able to follow along fairly easily, as we’re really just adding two elements to the flow.

Here’s what our final flow will look like:

  1. From Setup, navigate to Flows
  2. If you already have a Record-Triggered Flow for Opportunity Product, open that flow up
  3. Otherwise, click New and select Record-Triggered Flow
    1. The flow should be triggered when a record is created or updated
    2. It should run After Save
    3. Select Opportunity Product as the Object – there are no condition requirements
  4. Add a new Assignment Element to the flow. The purpose of this element is to collect individual records into a record collection variable that can be handed to the Perform Rollup Action.
    1. The label can be something like “Add records to collection variables”
  1. In Set Variable Values, click the Variable box and select “+ New Resource”
  2. Resource Type should be Variable
  3. API Name can be something like “CurrentRecords”
  1. Data Type is Record, and “Allow multiple values” checkbox should be checked off
  2. Check off the flag “Available for input” under the “Availability outside the Flow” header
  3. Save the resource, change the Operator field to “Add”, and use $Record (also represented as {!$Record}) as the value
  1. Repeat the above steps for another collection variable (can be called something like “PriorRecords”) and add {!$Record_Prior} to that second collection variable
  1. Add the Perform Rollup action to your flow and configure it:  
    1. click the plus sign on your Flow’s path, and select Action
    2. Scroll down on the left side till you see the “Rollups” category and click on it
    3. Select the “Perform Rollup__mdt-based Rollup” action
    4. Similar to step 4a in the “Creating Rollup Custom Metadata Type Records” section, Label can be whatever you’d like – but: we tend to organize our rollup labels using the convention “Operation Child Object Child Field to Parent Object Parent Field” – which, for our example, comes out to “Sum OLI Price to Opp Amount” for the label
  1. Object for “Prior records to rollup” (Input) and Object for “Records to rollup” (Input) should both be Opportunity Product, after which the additional fields will load
  2. Set “Rollup Context” to UPSERT
  3. Toggle the “Defer Processing?” field and add “{!$GlobalConstant.False}”
  4. Set “Prior Records to Rollup” to our “PriorRecords” collection variable
  5. Set “Records to Rollup” to our “CurrentRecords” collection variable that you created above.

The good news is that you’ve now successfully configured your first rollup!

Handling Record Deletions

 You’ll have to duplicate your work and create a flow that triggers on deletion of the records being rolled up(the only difference will be setting the “Rollup Context” field there to “DELETE”) in order to get everything up and running for all edge cases (Flow Orchestrator may help here!).

This is one of the only significant differences in setup for the Flow approach versus the Apex approach   because in Apex, a trigger can handle insert/update/delete all within one file., However, once your Flows have been setup, you don’t need to update them in order to add additional rollups, or to tweak your existing rollups; for that, all you need to do is modify your Custom Metadata Records. (If you add multiple rollups, it may make sense to relabel the first Rollup action to “Queue up Rollups for Opportunity Product” or something like that).

As you can see from the above screenshots, there are a significant number of optional arguments that you can use in Flow to further customize your rollups. Some of these serve as overrides for the individual Custom Metadata Records; the most commonly used option is the “SOQL Where Clause To Exclude Calc Items”, which allows you to only run rollups when records match the given where clause. For Rollups configured in the manner we’ve demonstrated above, it’s not recommended to use the “SOQL Where Clause To Exclude Calc Items” on the Action itself, but rather to configure those individually on the CMDT records. For more detail on the configuration options, look here.

Priming your Process With A One-Time ‘Backfill’ Rollup 

So, you’ve set up your rollups, as detailed above. There’s just one more step necessary. Apex Rollup calculates values for you in an ongoing fashion, which means that for all your pre-existing records, we need to backfill the correct values by performing a one-time calculation to assign the proper values to all parent records with configured rollups.

To do this, you’ll use the Rollup App that comes with this extension. This App isn’t normally going to be necessary because of the automation you’ve just created based on record creates and deletes, but it’s great for carrying out a one time manual recalculation.

  1. Click on the App Launcher
  2. Search for “Rollup”
    1. Click either “Rollup” under the Apps search result or
    2. Click the “Recalculate Rollup” item
  3. Both will bring you to the full recalculation Rollup application
  4. Because your rollup uses Custom Metadata Typess, toggle on the checkbox at the top of the page entitled “Run off of CMDT?”
  5. A combobox will appear where you can select individual rollup child types
  6. Once a child type is selected (like OpportunityLineItem), you can either choose to recalculate rollup values for each field one at a time, or, if you have multiple rollups per child object configured, all at once (by checking off all of the boxes)
  7. Click the “Start Rollup” button

And with that, we’re ready to roll … up! Once the calculation has finished, there’s no further maintenance required – as Opportunity Products are created / updated / deleted, their parent Opportunity’s Amount field will be kept in sync!

Further Reading

The Apex Rollup readme is filled with further information, and there is also a burgeoning Wiki being built out. Given that there are many, many possibilities for how to configure rollups, those two locations are the best place for finding more information about the configuration options available to you!