+ Start a Discussion
potziepotzie 

visual flow: is it possible to get the count of records in an sObject collection?

I have performed a fast lookup and retrieved a list of objects. I need to make a decision based on the number of records returned - can I do this with a formula instead of having to use a loop to count the records?
ShashankShashank (Salesforce Developers) 
I'm afraid the only way is to loop through thte records for count. This will also make for an interesting feature idea: https://success.salesforce.com/ideaSearch
Neha Saxena 10Neha Saxena 10
I did manage to get the Visualflow 's Loop to count the records.

I used the following exmaple to start:
https://developer.salesforce.com/docs/atlas.en-us.salesforce_vpm_guide.meta/salesforce_vpm_guide/vpm_designer_example_loop.htm

Step1: Create FastLookup where you are likely to get multiple records and assign it to a sObject Collection Variable. 
Step 2: Define a Variable of "Number" and put default value as 0
Step3: Get the Loop and assign the sObject Collection Variable.
Step4: Get "Assignment" and in the assignment section, select  the number variable setup in step2, and "Add" 1 to it. I believe when the variable is a number, it gives you option, equal, Add, substract. We need to use Add.
Step 5: Connect the Assignment back to Loop element
Step 6: Get the "End of Loop" and use that to either assign the Number variable to a field or update a field... I used update field element to get the count saved.

Hope this helps :)
 
Nicole Young 2Nicole Young 2
Hi Neha, Do you have screenhots of your elements? Mine isn't quite working and I just want to make sure I set it up correctly. Thanks!
Jenna Zelenetz 45Jenna Zelenetz 45
I had a scenario where records selected from a list view would be pulled into an sObject collection variable via a Visualforce page. There's no one-step way to evaluate whether the collection is empty (i.e. no records were selected) within the flow, but Neha's instructions did the trick! Here's a screenshot of the final setup (pardon the disorderly arrangement of the Start element - I tried to squeeze these 5 new steps in before the original start, which was the confirmation screen after the decision element):

Count collection with loop and assignment elements

In the "Next element" assignment element, I added 1 to a varCount number variable. In the "End of loop" assignment, I assigned the varCount value to a varTotal number variable. Then I evaluated varTotal in the decision element to display an error "No records selected" screen if varTotal = 0, or a default outcome confirmation "Are you sure you want to [execute flow actions] on [varTotal] records?" screen if varTotal not equal to 0. Works great!
DevWannabeDevWannabe
If you are just checking to see if there are records in the collection or not, you can do a decision block that checks to see if the collection is null or not.  If you need to do more than just see if it has records or not, i.e. need to know the exact count, then we are stuck with the loop/count process.  Go vote for this idea to see if we can the votes up enough to get it on the list... https://success.salesforce.com/ideaView?id=08730000000Dgp6AAC
athimathi sachithanandamathimathi sachithanandam
I created similar flow to count items and sum the Amount of all opportunity and display in screen. It is exiting after 20 count. Do u know why?
DevWannabeDevWannabe
athimathi, can you proivde a screenshot of the flow?
AthiAthi
sorry for late reply..

Here is the visual page calling flow

<apex:page standardController="Opportunity" tabStyle="Opportunity" recordSetVar="AllOpportunities" > <!-- Add below each field you reference in your Flow --> <apex:repeat value="{!AllOpportunities}" var="row" rendered="FALSE"> {!row.Amount} </apex:repeat> <!-- Runs your Flow --> <flow:interview name="Opp_Batch_Total" finishLocation="{!$CurrentPage.parameters.retURL}"> <!-- finishLocation="{!URLFOR($Action.Opportunity.Tab, $ObjectType.Opportunity)}"> --> <apex:param name="lst_Opp" value="{!AllOpportunities}"/> </flow:interview> </apex:page>



flow image

 
Joseph Nagy 7Joseph Nagy 7
Another thing to note if you're going through a lot of records you have potential to hit limits. So, One thing you could do if you're concerned about limits:

Build loop like you said:


Step1: Create FastLookup where you are likely to get multiple records and assign it to a sObject Collection Variable. 
Step 2: Define a Variable of "Number" and put default value as 0
Step3: Get the Loop and assign the sObject Collection Variable.
Step4: Get "Assignment" and in the assignment section, select  the number variable setup in step2, and "Add" 1 to it. I believe when the variable is a number, it gives you option, equal, Add, substract. We need to use Add.
Step 5: Create Decision that if count gets to X number then exit loop and do what you were going to do anyway. 

For example, what I am using this for is to find a renewal opportunity for a specific state under a specific customer. Most times this will only be a few records, but for our larger clients we could have 100s. So, if there are multiple I want to have the user choose the appropriate one, so I have a loop to begin the count, but as soon as it hits 2 I exit the loop and use a dynamic record choice with the same criteria as my fast lookup. This way it will never go through the loop more than twice and I won't hit my limits.
David Walker 66David Walker 66
Looping to count a collection is no longer required.  Since Summer '18 update you can now count directly - see here: https://jenwlee.com/2018/06/26/blink-you-may-have-missed-this-hidden-gem-get-count-via-flow-assignment/ (https://jenwlee.com/2018/06/26/blink-you-may-have-missed-this-hidden-gem-get-count-via-flow-assignment/" style="color:#0563c1; text-decoration:underline)
Evon MispetsEvon Mispets
Emil, sure you can. Do the following:

1. Create a number variable (vCount) and give it a default value of 0.
2. Use a loop element that loops through the Collection variable. 
3. Using an Assignment element, do: vCount + 1. Connect 2 and 3.
4. Connect the assignment element back to Loop element.
5. And that's it.
https://www.smular.com/
J KellyJ Kelly
You can now use the Equals Count operator in an Assignment element.  e.g.,

collectionVariableCountNumberVariable   Equals Count   collectionVariable