Build Record Map from Lookup

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 RecordMap 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:

  1. Use Get Records to retrieve a collection of Contacts

  2. Use a Loop over the collection of Contacts

  3. 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:

  1. Use Get Records to retrieve a collection of Contacts

  2. Use Build Record Map from Lookup with the Contacts as input to get a RecordMap of related Accounts using AccountId as the lookupField and store the RecordMap to an output variable.

  3. Use a Loop over the collection of Contacts

  4. Use Get Record from Record Map 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.






A collection of input records with lookup fields




The lookup field on each input record containing the reference to the record you want to put in the RecordMap. e.g. AccountId or OwnerId.




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.




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 RecordMap will be Account records and the key will be the Id of each Account.

Last updated