Last Updated on August 2, 2023 by Rakesh Gupta
Salesforce Flow allows us to automate business processes by building applications, known as Flows. Using Flows, a user can collect information; or, they can update, edit, or create records in Salesforce. Furthermore, Flows can execute logic, interact with the Salesforce database, call Apex classes, Platform Events, and guide users through various screens to streamline the process of collecting and updating data.
Once a Flow is built, an Admin can make the Flow available to the right users or systems.
If you have a basic understanding of Apex then, you can write a query to get records and count the list size. For example, using Apex, one can get all Contacts that belong to city Alpharetta and, count list size, by writing the following code:
List<Contact> myContact = new List<Contact>(); myContact = [Select id, Email from contact where Mailingcity ='Alpharetta']; system.debug('Size of List'+myContact.size());
Now suppose you wonder, can I achieve the aforementioned outcome by using Flow? Well, you are in luck! For, before the recent release, it was very complex to count the records in a Record Collection Variable (Kind of equivalent to Apex list).
But, since Spring’18 release, a user can use an Assignment element to count the Record Collection Variable size and store that number in a variable without using the Loop element!
Business Use Case
Edward Backhouse is working as a System administrator at GurukulOnCloud. He received a requirement to count Contact records where Mailing city equals Alpharetta. The new feature of Flow to rescue!
The solution for the above business requirement
There are a few possible solutions for the above business scenario. One of them, of course, is to continue to use the old method, as shown in the preceding screenshot.
But, Edward is a Salesforce Ninja-like us! And, therefore, he is always on the lookout for feature enhancements. As a result, Edward knows that, after Summer’18, he can solve the above requirement efficiently! So, instead of resorting to the old method, he takes following the steps:
- Edward creates a Flow diagram using Draw.io (or LucidChart).
- Next, he navigates to Setup (Gear Icon) | Setup | Process Automation | Flows in Lightning Experience.
- Then he clicks on the New Flow button – which, in turn, opens the Flow canvas.
- Edward creates a Variable (to store record count) as shown in the following screenshot:
- The next step is to use the Get Records element to get all contact records where MailingCIty is Alpharetta. Edward clicks on the Palette tab and drag and drops the Get Records element onto the canvas; this opens a new window. Next, he maps the Record Create element according to the following screenshot:
- The next step is to use an Assignment element to count the record in a Record Collection Variable. Edward clicks on the Palette tab and drag and drops the Assignment element onto the canvas; this opens a new window. Next, he maps the Record Create element according to the following screenshot:
- In the end, Edward’s Flow will look like the following screenshot:
- Basking in his success, Edward Saves the Flow (Type: – Screen Flow) and named it Record Counts in SOC. Finally, he and closed the canvas.
- As soon as he runs the Flow, voila! He gets the count of Contact records where Mailing City equals Alpharetta!
You too can follow Edward’s footsteps literally and figuratively! Just like Edward, keep an eye out for feature enhancements – such as the one we just discussed above. Don’t want to miss out on any enhancement? Then, visit https://automationchampion.com/ often to keep yourself abreast of any and all developments!
But for now, go ahead! Show off your newly acquired skills to your friends, colleagues, and families!
👉 Check out the video for step-by-step instructions.
Formative Assessment:
Please feel free to add constructive comments, insights – and yes, challenges too! – to the blog. Good Luck in your Journey towards becoming a Salesforce Ninja!
sir can you please share a process of sum amount of opportunity related to account
To address the business requirement, using a Roll-up summary field would be ideal. Is there a particular reason for using Flow?
The last step to display the record count is not mentioned. It is simple, here are the steps;
1. Add a display screen
2. Drag Display text component in to display screen
3. Name your display text component
4. Under Insert a Source text box paste this code Total Count: {!vNumberCount}
I’m confused. what information goes into Display screen element, to get the flow to display the “total count: 2” ??
Use this
Total Count – {!VarNCount}
How to tackle the Get Record element when no records are returned?
I am using this in my flow and when no records are returned (means the collection variable is null), equals count still counts the null value as 1. This is causing my flow to fail because I need to delete records return by Get records
Strange, check the default value of Count (Number Variable). Would you mind sharing the screenshots of your Flow?
To make this solution more sustainable/scalable, you can keep the “get records” element as open as possible, and then add “Collection Filter” elements after it to add criteria and get a subset of the record collection.
This way, you can have multiple branches in your flow for taking different action on different subsets of records in the collection.
Branch 1: all Contacts that belong to city Alpharetta
Branch 2: all Contacts where “Email Opt Out” = FALSE
…etc.
Count existing Contact Records in Account Record.
This doesn’t account for records being deleted. If one of the contact records gets deleted the count wont update. How would you account for this?
I am using Flow Builder now, How can I achieve this in th Flow Builder?
Great post Rakesh. Super helpful as always. Seriously, Salesforce should be paying you for the all the detailed documentation you have put out! With only Salesforce documentation, I literally never would have been able to accomplish what I needed.
Hey Matt, thanks for the great comment. Hearing stuff like this makes my day.
Great post!
This will be useful for the Maintenance Credentials on Trailhead updating the App Builder Certification.
Thanks