You need to sign in to do that
Don't have an account?
potzie
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?
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 :)
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!
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>
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.
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/
collectionVariableCountNumberVariable Equals Count collectionVariable