Build Record Map from Lookup
Last updated
Last updated
Takes a collection of records as input
and the name of a lookup field specified as lookupField
. This will then query for all the related records of the object specified by the lookupField
. The fields loaded for the related records are defined by selectFields
. This provides an efficient way to query for a large number of related records at once.
To use the later in your Flow, make sure to select Store Output Variables
and assign it to a variable resource with a data type of Apex-Defined
and an apex class of GradientWorks__RecordMap
. When creating the resource, do not check "Allow multiple values (collection)".
For example, consider the following Flow structure:
Use Get Records
to retrieve a collection of Contacts
Use a Loop
over the collection of Contacts
Use Get Records
inside the loop to retrieve the Account related to each Contact using Contact.AccountId
This works, but performs an additional SOQL query for each Contact. If you have 50 Contacts, your Flow will perform 51 queries. One to get the original Contact list and then one per Contact. You will rapidly hit Salesforce governor limits and your Flow may run slowly.
This action allows for a pattern like so:
Use Get Records
to retrieve a collection of Contacts
Use with the Contacts as input
to get a of related Accounts using AccountId
as the lookupField
and store the to an output variable.
Use a Loop
over the collection of Contacts
Use inside the loop with Contact.AccountId
as the key to get the related Account
While this requires an extra step, it only uses 2 SOQL queries: one to get the list of contacts and one to retrieve all the related accounts. Your flow will be much less likely to hit governor limits and will run more quickly.
input
Yes
List<SObject>
A collection of input records with lookup fields
lookupField
Yes
String
selectFields
Yes
String
A comma-delimited list of fields on the related object you want to include. e.g. Id, Name, CustomField__c
. You must specify at least one field.
recordMap
The lookup field on each input record containing the reference to the record you want to put in the . e.g. AccountId
or OwnerId
.
Contains records of the type referenced by the lookup field. The keys are the Ids of the related records. e.g. If your lookupField
is a lookup for Account
, the records in the will be Account
records and the key will be the Id
of each Account
.