Get records in a Flow using a collection
Updated: Sep 14, 2022
Update 08.2022: IN and NOT IN will be available in Winter '23, but without possibility to use records on the right side of the IN operator.
Link to the app exchange listing:
As we all know, Salesforce decided to retire Process Builders and fully "go with the flow (s)". If you have not completed your migration to Flows, you will need to do it sooner or later. It seems that Salesforce invests a lot of time and effort to make Flows better and better, and in fact they are extremely powerful - Apex code can be replaced using features like Loops, Collection Filters or Update operations.
It doesn't matter if you are a Flow master or not yet - you probably noticed the lack of getting records or filtering using a collection. For those who are familiar with Apex, this is the simple, corresponding syntax:
SELECT ... FROM Account WHERE Id IN: collectionOfIds
The Idea to deliver this functionality has a quite significant number of votes (more than 17k!) https://ideas.salesforce.com/s/idea/a0B8W00000GdhBKUAZ/flow-support-equivalent-of-soql-in-condition-in-record-filters
Without this functionality, you have to get records in a loop, but this is definitely a bad practice and very quickly you can hit the limits. Let's take a look at this simple example:
To solve this issue, we created the app which contains apex actions which allow you to filter by collections (either ID or IDs extracted from sObject list). You can find other workarounds on the internet but they are either error prone, complex or they require custom changes to adjust their functionalities. Our objective was to keep it as simple as possible and hide the entire back-end logic in Apex. The only thing you need to do is to call the Action in your Flow and properly pass a few parameters:
Here we are going to provide a step-by-step instruction how to use the app.
example: Based on a Opportunity Collection, get all related Accounts:
First of all, you need to call FlowCollectionFilterAction:
Then you need to set following parameters:
(take a look at the green numbers on the right side)
1 - type of the sObject you would like to use to get related records from (in this case, Opportunity)
2 - the same as number 1
3 - type of the sObjects you would like to return (in this case, Account)
4 - type of the sObject you would like to use to get related records from (in this case, Opportunity) - same as number 1 and 2
5 - the Opportunity field you want to use for finding returned sObject (in this case, AccountId)
6 - type of the returned sObject (Account)
7 - map field from a source object (AccountId on Opportunity) to a result sObject (Id on Account)
8 - source collection records - collection of Opportunities for which we want to get all related Accounts using the AccountId field
in short (now take a look at Input Values numbers on the left side): Using all AccountIds (2) taken from Opportunity collection record (5), find all Accounts (3) where Opportunities.AccountId = Accounts.Id (4)
9 - collection of Account records; only records belonging to this collection will be returned (not all of the records in the system)
in short: Using all AccountIds (2) taken from Opportunity collection record (5), find all Accounts (3) where Opportunities.AccountId = Accounts.Id (4) AND Accounts.Id belongs to OutputCollection.Ids (6)
The last thing is to store the output Accounts in a variable:
And... that's all!
The action saves all fields values by default.
example: Based on a collection of IDs , get all related Accounts:
If you already have a collection of record IDs (for example you can create such collection by looping over records and add each ID to the collection separately), it is even easier:
1 - this is not relevant, but required in the code; you can put whatever type you want, eg Opportunity
2 - type of the returned sObjects (in this case, Account)
3 - the same as number 2
4 - collection of records IDs (in this case, Account IDs)
5 - collection of Account records; only records belonging to this collection will be returned (not all of the records in the system)
in short: Return all Accounts (3) where Accounts.Id belongs to passed collection of IDs (4)
Again, you need to store the action's output in a collection variable.
If you are curious how we can help you with your Flows or custom business process automation, feel free to contact us.