Count Records in Collection Using Salesforce Flow : Bijay Kumar
by: Bijay Kumar
blow post content copied from SalesForce FAQs
click here to view original post
### Summary of Counting Records in Salesforce Flow In Salesforce Flow, counting records in a collection variable is often necessary for various business needs, such as sending alerts based on the number of related records. This guide provides a step-by-step approach to counting records using Salesforce Flow, illustrated with practical examples. #### Key Details 1. **Purpose**: Counting records helps in decision-making within a flow, such as displaying messages or triggering alerts based on the number of records retrieved. 2. **Example 1 - Counting Active Accounts**: - Create a Screen Flow to count active accounts. - Use a collection variable to store the active accounts. - Set up a variable to hold the count and display it on the screen. - Implement a decision element to show a message if the count exceeds 20. 3. **Example 2 - Counting Account-Related Opportunities**: - Create a flow to count opportunities related to a specific account. - Use a variable to store the account ID. - Fetch opportunities and count them, displaying the count on the screen. - Update the account's priority based on the number of opportunities (more than 3 for High priority, otherwise Low). 4. **Steps to Create a Flow**: - Navigate to Setup and create a new Screen Flow. - Define variables for storing records and counts. - Use elements like Get Records, Assignment, Decision, and Screen to build the flow. - Debug and activate the flow after testing. 5. **Conclusion**: The article emphasizes the importance of counting records in Salesforce Flow for effective business automation and decision-making. ### Additional Context Salesforce Flow is a powerful tool that allows users to automate processes without needing extensive programming knowledge. By counting records, businesses can streamline their operations, ensuring that actions are taken based on real-time data. ### Hashtags for SEO #Salesforce #SalesforceFlow #BusinessAutomation #CountRecords #SalesforceTutorial #SalesforceDevelopment #ScreenFlow #CRM #SalesforceTips #SalesforceTraining
While creating a Salesforce Flow, you may sometimes need to count how many records are stored in a collection variable. This is a common requirement in many business scenarios.
For example, you may want to send an alert if there are more than 5 cases related to an Account, or show a message if no related records are found.
In this article, we will learn how to count records in collection using Salesforce Flow with step-by-step instructions and a real-time example.
Why Count Records in a Collection Using Salesforce Flow?
In Salesforce Flows, when we use a get records element, we often fetch multiple records from an object at once. These records are stored in a collection variable. After that, sometimes we need to do the following things with the fetched records:
- Show the number of related records on the screen?
- Check if the count is more than or less than a specific number?
- Decide whether to proceed with a Flow based on the count?
Count Records in Collection Using Salesforce Flow
Below, I will explain how to create a flow to count the number of records in a Salesforce collection.
Example: Count the Active Accounts and Display a Message
Let’s say we want to create a flow that retrieves all active accounts, counts the number of those accounts, and then displays the count on the screen. If the count exceeds 20, an excellent message should display on the screen.
Go to Setup -> in Quick Find search for ‘Flows‘ -> click the New Flow button -> select Start From Scratch -> Screen Flow -> Create.
Here we want to display the total number of Active accounts from the record collection, so we need to use the Screen Flow.

First, we need to create a new resource variable to store the account records.
For that, go to New Resource -> in the Resource Type select Variable -> provide API Name -> choose Data Type as Record -> check the Allow multiple Values (collection) checkbox -> choose the Object to which you want to store the records -> click the Done button.

Then, click the + icon (Add Element) to add the Get Record element, as we need to fetch the active account records.
Provide the Label and select the Object from which you want to fetch the records. After that, provide the condition as per your requirement. Here, I want only the active account records.

After that, as you scroll down, you need to select ‘All Records‘ because we want to store it in the collection variable that we created.
Then we need to select the ‘Choose fields and assign variable‘ option to add the fetched records to the collection variable.
After that, in the Record Collection, we need to select the variable that we created.

Now, to store the number of records we have, we need to create a variable.
Here, I have created a variable with ‘Number‘ as the Data Type and assigned 0 as the default value.

Now, to display the record count, we need to assign the list or record collection to the number variable that we created in the previous step.
For that, we need to add the Assignment element, and in the Set Variable Values section, we need to use a numeric variable. We have the Equal Count operator to assign the count to the variable, and in the value, select the record collection.

After obtaining the count of records in our record collection, we have a condition to display a message based on the number of records we have.
For that, we need to add the Decision element. Here, I added the condition that if the counter (number variable) is greater than or equal to 20, then the next step should be executed.

Now, to display the count of records, we need to add the Screen element and Display Text component to the screen.
Then, from the ‘Insert a Resource‘ section, we need to add the number variable that we created to display the count.

After that, click the Save button. First, we need to debug the flow to test it before activating.

After saving the flow, we need to debug it. To do this, click the Debug button, and as shown in the image below, we have 56 active account records.

To check the number of active accounts, go to the Account object and filter the List View to display only active accounts.

In the image below, you can see that the number of active accounts is the same as the number of accounts displayed in the screen flow.

In this way, we can count the records in the collection using Salesforce Flow.
Example: Count the Account-Related Opportunities Using Salesforce Flow
For example, we want to count the opportunities related to the account. If any account has more than 3 opportunities, we need to update the account priority to High. Conversely, if the account has fewer than 3 opportunities, we need to update the account priority to Low.
Now let’s create the flow to count the related records using a flow in Salesforce. Here, we need to create a Screen Flow to display the opportunity count. Then, we need to deploy the flow on the account record page so that we can retrieve the related opportunities of an account.
Go to Setup -> in Quick Find search for ‘Flows‘ -> click the New Flow button -> select Start From Scratch -> Screen Flow -> Create.
First, create a Variable named ‘recordId‘ to get the account ID from the record and ensure that the Available for input checkbox is checked.

After that, we need to fetch the opportunity records so that we can count opportunities related to an account.
To access opportunities related to a particular account, we need to add a filter and specify the condition that the Account ID from the opportunity matches the record ID variable that we created.

After that, create a new variable of the Number data type to count the related records. Initially, the default value is set to zero, so if there is a related opportunity, it will be incremented; otherwise, it will display zero related record count.

Then add the Assignment element to count the related opportunities of an account and assign that count to the counter variable that we created in the above step.
Here, we need to use the Equal Count operator, and in the value, we need to provide the Get record element, which is a collection of opportunity records.

After that, add a Screen element and add the Display Text component to the screen, and add the counter variable where we assigned the number of related opportunities.

We also want to adjust the priority according to business needs. After obtaining the opportunity count for an account, to change the account priority, we need to update the priority field value. For that, I have changed the next button label to Change Priority.

Now add the Decision element because, as per our scenario, we have two conditions: if any account has more than 3 opportunities, we need to update the account priority to High. Conversely, if the account has fewer than 3 opportunities, we need to update the account priority to Low.

Then, create a new variable to access the account fields, using Record as the data type and selecting the Account object.

Again, add the Assignment element, and then we need to get the record ID of the account, and then assign the priority to the account priority field.
Similarly, we need to add another assignment element for the low-priority field.

After that, the value we have assigned using the assignment element, we need to add the Update Record element to update the Account Priority field on the account object.

Then save it, and before activating, first Debug the flow to test its working, then Activate it.

After activating the flow, we need to deploy the flow to the Account record page and provide the recordId variable to the input value.
Below, you can see that the screen component we deployed is displaying the related opportunity counts for the opened account.
As we click the Change Priority button, the account priority changes as per the business logic we provided in the decision element.

In this way, we can count the related record count using the flow in Salesforce.
Conclusion
I hope you have got an idea about how to count records in a collection variable using Salesforce Flow. I have explained a real-world example that we can use for business automation.
In the first example, we show the active account counts, and then in the next example, I explain how to display the opportunity count of related accounts, and if the opportunities are more than three, then as we click the change priority button, it changes the priority of the account as per business requirement.
You may like to read:
- Display Records in Lightning Data Table Using Salesforce Screen Flows
- How to Pause and Resume a Screen Flow in Salesforce
- How to Use the Repeater Component in Salesforce Screen flow
The post Count Records in Collection Using Salesforce Flow appeared first on SalesForce FAQs.
June 08, 2025 at 08:43AM
Click here for more details...
=============================
The original post is available in SalesForce FAQs by Bijay Kumar
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================

Post a Comment