# 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. |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.gradient.works/kb/automation-builder-kit-abk/actions/advanced/gwfxexecutesoqlqueryaction.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
