# Execute SOQL

{% hint style="warning" %}
This is an advanced action. If your query is not specific enough, your flow may run slowly or fail. Only use this if you have a thorough understanding of SOQL and the built-in Get Records action is too restrictive to do what you need.
{% endhint %}

Takes a [SOQL query](https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql.htm) and executes it within a Flow, retrieving the records according to the specified query. This provides a useful tool for getting around some of the limitations of the built-in `Get Records` action.

For example, consider the requirement to fetch a list of Users based on whether their Role name contains the word "Sales". Using `Get Records` will require you to use OR conditions with hard-coded Role Ids. Using this action, you can use SOQL's built-in [relationship queries](https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships.htm) like so:

```sql
SELECT Id, Name, Email FROM User WHERE UserRole.Name LIKE '%Sales%'
```

Other use cases for this include IN/NOT IN queries or LIMIT clauses to retrieve only a subset of records.

To use the results, you'll need to store the output in a variable of type Record with the Object Type set to the object you're querying (e.g. User in the example above). Be sure to specify that the variable is a Collection.

When you use records retrieved by this action, only the fields you specify will be available.

## Inputs

| Name  | Required | Type   | Description                   |
| ----- | -------- | ------ | ----------------------------- |
| query | Yes      | String | The SOQL query to be executed |

## Outputs

| Name    | Type           | Description                                                                                                                                        |
| ------- | -------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- |
| records | List\<SObject> | The collection of records retrieved by the SOQL query. The object type of these records is determined by the FROM clause in the SOQL query itself. |
