# 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](https://docs.gradient.works/kb/automation-builder-kit-abk/models/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](https://docs.gradient.works/kb/automation-builder-kit-abk/actions/advanced/gwfxbuildrecordmapfromlookupaction) with the Contacts as `input` to get a [RecordMap](https://docs.gradient.works/kb/automation-builder-kit-abk/models/recordmap) of related Accounts using `AccountId` as the `lookupField` and store the [RecordMap](https://docs.gradient.works/kb/automation-builder-kit-abk/models/recordmap) to an output variable.
3. Use a `Loop` over the collection of Contacts
4. Use [Get Record from Record Map](https://docs.gradient.works/kb/automation-builder-kit-abk/actions/advanced/gwfxrecordmapgetaction) 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.

## Inputs

| Name         | Required | Type           | Description                                                                                                                                                                                                                 |
| ------------ | -------- | -------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| input        | Yes      | List\<SObject> | A collection of input records with lookup fields                                                                                                                                                                            |
| lookupField  | Yes      | String         | The lookup field on each input record containing the reference to the record you want to put in the [RecordMap](https://docs.gradient.works/kb/automation-builder-kit-abk/models/recordmap). e.g. `AccountId` or `OwnerId`. |
| 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.                                                                           |

## Outputs

| Name      | Type                                                                                    | Description                                                                                                                                                                                                                                                                                                                                          |
| --------- | --------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| recordMap | [RecordMap](https://docs.gradient.works/kb/automation-builder-kit-abk/models/recordmap) | 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](https://docs.gradient.works/kb/automation-builder-kit-abk/models/recordmap) will be `Account` records and the key will be the `Id` of each `Account`. |
