# SOQL Query — Salesforce

> Execute a SOQL query against Salesforce. This is the primary tool for querying Salesforce data — use for all structured queries. For free-text search across multiple objects, use Text Search instead.

- Key: `salesforce_rest_api-soql-query`
- Type: Action (Read-only)
- Version: 0.0.4
- App: Salesforce (`salesforce_rest_api`) — https://pipedream.com/apps/salesforce-rest-api.md
- This page (HTML): https://pipedream.com/apps/salesforce-rest-api/actions/soql-query
- Hints: read-only · open-world
- Source: https://github.com/PipedreamHQ/pipedream/blob/master/components/salesforce_rest_api/actions/soql-query/soql-query.mjs

## Description

Execute a SOQL query against Salesforce. This is the **primary tool for querying Salesforce data** — use for all structured queries. For free-text search across multiple objects, use **Text Search** instead.

**When the user uses first-person language ('my', 'I', 'me'),** filter by `OwnerId` using the `userId` from **Get User Info**. Use **Describe Object** to discover field names and picklist values before querying non-obvious fields.

**SOQL syntax reference:**
- Basic: `SELECT Id, Name, Email FROM Contact WHERE AccountId = '001xxx'`
- Operators: `=`, `!=`, `>`, `<`, `>=`, `<=`, `LIKE '%text%'`, `IN ('a','b')`, `NOT IN`
- Date literals: `TODAY`, `THIS_MONTH`, `LAST_N_DAYS:30`, `THIS_QUARTER`, `LAST_QUARTER`, `THIS_YEAR`
- NULL checks: `WHERE Email != null`
- Aggregates: `SELECT StageName, COUNT(Id) c, SUM(Amount) s FROM Opportunity GROUP BY StageName`
- **SOQL does NOT support the `AS` keyword** — write `COUNT(Id) c`, not `COUNT(Id) AS c`
- **Reserved words cannot be aliases** — `count`, `sum`, `avg` are reserved. Use short aliases like `c`, `s`, `a`
- **Cannot ORDER BY alias** — repeat the aggregate: `ORDER BY COUNT(Id) DESC`, not `ORDER BY c DESC`
- Parent relationship: `SELECT Name, Account.Name FROM Contact`
- Child subquery: `SELECT Name, (SELECT Name FROM Contacts) FROM Account`
- Sorting/limits: `ORDER BY CreatedDate DESC LIMIT 10`

**Always include `Id` in SELECT.** Include a clickable Salesforce link for every record using the format `{instanceUrl}/lightning/r/{objectType}/{Id}/view` (get `instanceUrl` from **Get User Info**). [See the documentation](https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/resources_query.htm)

## Props

| Prop | Type | Required | Description |
|---|---|---|---|
| `query` | `string` | Yes | The SOQL query string to execute. Example: SELECT Id, Name, Amount, StageName FROM Opportunity WHERE OwnerId = '005xxx' AND StageName = 'Closed Won' ORDER BY Amount DESC LIMIT 10 |

## Run it

**MCP**

```ts
import { Client } from "@modelcontextprotocol/sdk/client/index.js"
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js"
import { PipedreamClient } from "@pipedream/sdk"

const pd = new PipedreamClient({
  projectId: process.env.PIPEDREAM_PROJECT_ID!,
  clientId: process.env.PIPEDREAM_CLIENT_ID!,
  clientSecret: process.env.PIPEDREAM_CLIENT_SECRET!,
  projectEnvironment: "production",
})

const accessToken = await pd.rawAccessToken

const transport = new StreamableHTTPClientTransport(
  new URL("https://remote.mcp.pipedream.net/v3"),
  {
    requestInit: {
      headers: {
        Authorization: `Bearer ${accessToken}`,
        "x-pd-project-id": process.env.PIPEDREAM_PROJECT_ID!,
        "x-pd-environment": "production",
        "x-pd-external-user-id": "{external_user_id}", // any stable ID for this user in your system
        "x-pd-app-slug": "salesforce_rest_api",
      },
    },
  },
)

const mcp = new Client({ name: "my-agent", version: "1.0.0" })
await mcp.connect(transport)

const { tools } = await mcp.listTools()

// listTools() hands your model this tool's input schema, so it can
// fill the arguments itself:
const result = await mcp.callTool({
  name: "salesforce_rest_api-soql-query",
  arguments: {
    query: "SOQL Query",
  },
})
```

**TypeScript**

```ts
import { PipedreamClient } from "@pipedream/sdk"

const pd = new PipedreamClient({
  projectId: process.env.PIPEDREAM_PROJECT_ID!,
  clientId: process.env.PIPEDREAM_CLIENT_ID!,
  clientSecret: process.env.PIPEDREAM_CLIENT_SECRET!,
  projectEnvironment: "production",
})

const result = await pd.actions.run({
  id: "salesforce_rest_api-soql-query",
  externalUserId: "{external_user_id}", // any stable ID for this user in your system
  configuredProps: {
    salesforce_rest_api: { authProvisionId: "apn_xxxxxxx" },
    query: "SOQL Query",
  },
})

console.log(result)
```

**cURL**

```bash
curl -X POST https://api.pipedream.com/v1/connect/{project_id}/actions/run \
  -H "Content-Type: application/json" \
  -H "X-PD-Environment: production" \
  -H "Authorization: Bearer {access_token}" \
  -d '{
    "external_user_id": "{external_user_id}",
    "id": "salesforce_rest_api-soql-query",
    "configured_props": {
      "salesforce_rest_api": { "authProvisionId": "apn_xxxxxxx" },
      "query": "SOQL Query"
    }
  }'
```

---

- App: https://pipedream.com/apps/salesforce-rest-api.md · All apps: https://pipedream.com/apps
