Transform Elements Over Loops: Easily Sum or Count Items in Collections

Transform Elements Over Loops: Easily Sum or Count Items in Collections

Last Updated on February 5, 2025 by Rakesh Gupta

Big Idea or Enduring Question:

  • How can I use the transform element to easily perform operations like sum and count without using a loop?

Objectives:

After reading this blog, you’ll be able to:

  1. Utilize the Transform element to map and transform source data to target data.
  2. Count the number of items in a source collection.
  3. Aggregate data from a source collection to calculate the sum.
  4. And much more!

👉 Previously, I’ve written several posts about how to use the Transform element and its use cases. Why not check them out while you’re at it?

  1. Transform Elements Over Loops: A Modern Method for Collecting IDs in Salesforce Flow
  2. Transform Elements Over Loops: Create Tasks for Opportunities Without Loops
  3. Use Multiple Data Sources in Transform Element to Fetch Data from Multiple Objects

Business Use case

Warren Mason, a Junior Developer at Gurukul on Cloud (GoC), has been working closely with his mentor, Joe, to understand the Transform element. With Joe’s guidance, Warren has gained a solid understanding of its functionality and applications.

During a discussion with Joe about a separate issue, Warren shared a new requirement that could serve as an excellent use case for the Transform element. 

When an account is updated, find all related open opportunities and calculate the total sum of their opportunity amounts.

Automation Champion Approach (I-do):

In the past, I wrote a detailed article on how to perform a count in Flow using the Assignment element, which can be found Count Number of Records in a Record Collection Variable.

I also wrote a detailed article on using aggregate functions in Flow. You can read more about it Work With Aggregate Functions in Salesforce Flow. This article demonstrates how to achieve aggregation using loops and the Assignment element. The steps to solve it using loops and the Assignment element are provided in the screenshot below.

Now, Salesforce natively supports aggregation (sum) through the Transform element. This article will focus on how to use the Transform element to perform aggregate sums. Additionally, you can use the Transform element to count the number of items in a source collection.

As of Spring ’25, the Transform element is only available with after-save flows and not with before-save record-triggered flows.

Guided Practice (We-do):

There are 3 steps to solve Warren’s business requirement using Record-triggered After-Save Flow. We must:

  1. Define flow properties for a record-triggered flow.
  2. Add a Get Records element to find related open opportunities.
  3. Add a Transform element to calculate the sum of opportunity amounts.

Step 1: Define Flow Properties

  1. Click Setup.
  2. In the Quick Find box, type Flows.
  3. Select Flows then click on the New Flow.
  4. Select the Start From Scratch option and click on Next.
  5. Select the Record-Triggered Flow option, click on Create
    1. Object: Account
    2. Trigger the Flow When: A record is created or updated
    3. Optimize the Flow For: Action and Related Records 
  6. Click the X at the top to save your changes.

Step 2: Adding a Get Record Element to Find Related Open Opportunities  

The next step is using the Get Element to find related open opportunities. We will apply the filter logic to find the related open opportunities here instead of using an additional element.

  1. On Flow Designer, click on the +icon and select the Get Records element.
  2. Enter a name in the Label field; the API Name will auto-populate.
  3. Select the Opportunity object from the dropdown list.
  4. Select All Conditions Are Met (AND). 
  5. Set Filter Conditions
    1. Row 1:
      1. Field: AccountId
      2. Operator: Equals
      3. Value: {!$Record.Id}
    2. Click Add Condition
    3. Row 2:
      1. Field: IsClosed
      2. Operator: Equals
      3. Value: {!$GlobalConstant.False}
  6. How Many Records to Store:
    1. Select All record
  7. How to Store Record Data:
    1. Choose the option to Choose fields and let Salesforce do the rest. 
      1. Select Opportunity Fields to Store in Variable
        1. Id
        2. Name
        3. StageName
        4. AccountId
        5. OwnerId
  8. Click the X at the top to save your changes.

Step 3: Add a Transform Element to Calculate the Sum of Opportunity Amounts

Now, we will add a Transform element to calculate the total sum of the amounts from all related open opportunities. This step is crucial for aggregating the data efficiently without using loop.

  1. On Flow Designer, click on the +icon and select the Transform element.
  2. Enter a name in the Label field; the API Name will auto-populate.
  3. For Source Data select {!Get_Open_Opportunities}.
  4. For Target Data select the following options:
    1. Data Type: Currency
    2. Allow multiple values (collection):  Unchecked
    3. Click Create
  5. Map the Get Open Opportunities (Source Data) to the SumOfOpportunityAmounts (Target Data). A pop-up will appear to configure the aggregate function.
    1. Source Collection: Get_Open_Opportunities
    2. Aggregate Type:  Sum
    3. Field to Transform:  Amount
  6. Once done, click on the <– arrow to return to the Flow designer.
  7. Click the X at the top to save your changes.

In the end, Warren’s Flow will look like the following screenshot:

Once everything looks good, perform the steps below:

  1. Click Save.
  2. Enter the Flow Label the API Name will auto-populate.
  3. Click Show Advanced.
  4. API Version for Running the Flow: 63
  5. Interview Label: Summation Using Transform {!$Flow.CurrentDateTime}
  6. Click Save.

Almost there! Once everything looks good, click the Activate button.

Proof of Concept

I am using the On-Screen Debug feature in Flow to verify the logic’s functionality and ensure that the flow is executing as expected. This involves checking whether the calculations are accurate and confirming if the flow is correctly returning the desired sum.

Below is a screenshot from the account that shows open opportunities and their amounts.

Formative Assessment:

I want to hear from you!

What is one thing you learned from this post? How do you envision applying this new knowledge in the real world? Feel free to share in the comments below.

Go back

Your message has been sent

Warning
Warning
Warning
Warning
Warning

Warning.

2 thoughts on “Transform Elements Over Loops: Easily Sum or Count Items in Collections

  1. How do you standardise the values if the opportunities are different currencies? For example, if we had opportunities in EUR, GBP and USD, and want to summarise it in USD? Can you do the same on the convertcurrency() version of the field? The amount(converted) doesn’t appear selectable in the transform step.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.