# SOQL Query Assistant

> This skill teaches an AI assistant to turn a plain-language question about Salesforce data into a correct, read-only SOQL query. It discovers real field API names first, runs the query through a Salesforce MCP server or the sf CLI, and explains the result. It never changes data.

Category: Data and queries · Version 1.0.0 · Updated 2026-11-17 · MIT license
Page: https://sfdxtalk.com/skills/soql-query-assistant/
Download: https://sfdxtalk.com/skills/soql-query-assistant.zip
Raw skill file: https://sfdxtalk.com/skills/soql-query-assistant/SKILL.md

## What this skill does

- Discovers objects and field API names with the MCP getObjectSchema tool or sf sobject describe before it writes a query.
- Writes SOQL with parent fields, child subqueries, date literals and aggregates, and fixes queries that fail.
- Runs a bounded first query, then reports the row count and the key findings.
- Warns about patterns that are often slow on large objects, such as leading wildcards and negative filters.
- Falls back to writing the query without running it when no connection is available.

## When to use it

- You want to find, count or list records and do not remember the exact field names.
- A query fails with an invalid field or relationship error and you want it fixed.
- You want a query explained, tightened or checked before it goes into Apex.
- You want an assistant to answer data questions without any risk of it editing records.

## What you need

- Connection: A Salesforce MCP server that exposes query tools (for example the hosted platform/sobject-reads server) or the Salesforce CLI logged in to an org. Without a connection it still writes queries but cannot run them.
- Permissions: Read access to the objects and fields you ask about. Use a read-only server or permission set, and a sandbox first. The connection enforces your profile, permission sets and field-level security.

## How it works

1. It clarifies the object, filters and expected result size, and asks one short question if something is ambiguous.
2. It inspects the schema with getObjectSchema (MCP) or sf sobject describe (CLI) so every field name is real.
3. It writes the query using a checklist for correctness and efficiency, with named fields, bound values and a LIMIT.
4. It runs a bounded version first (soqlQuery on MCP, or sf data query), then the full query if needed.
5. It reports the query, the row count and the findings, and says if a limit truncated the result.
6. If the query errors, it reads the message, fixes the query and runs it again.

## Safety rules built into the skill

- Read-only: it never runs DML, anonymous Apex or any tool that creates, updates or deletes records.
- It never invents field names. A field missing from the schema result is treated as unreadable or non-existent.
- It does not work around field-level security.
- It treats query results as sensitive and does not repeat personal data beyond what you asked for.
- It flags non-selective patterns instead of silently running them.

## Try asking

- How many open Opportunities over $50,000 closed in the last quarter, grouped by owner?
- List the Accounts with no Contacts, newest first. Show the first 20.
- This query fails with "No such column": fix it. SELECT Id, Total_Amount__c FROM Opportunity
- Which Cases are older than 30 days and still open? Write the SOQL and run it.

## SKILL.md

````markdown
---
name: soql-query-assistant
description: Writes, checks and runs read-only SOQL queries against a connected Salesforce org. Use when the user asks to find, count, list, export or analyze Salesforce records, wants a SOQL query written or fixed, or asks why a query is slow or fails. Discovers real field API names before writing the query and never changes data.
license: MIT
metadata:
  version: "1.0.0"
  publisher: SFDX Talk (sfdxtalk.com)
---

# SOQL Query Assistant

Turn a plain-language question about Salesforce data into a correct, efficient, read-only SOQL query, run it through the org connection, and explain the result.

## Connection

Use whichever connection is available, in this order:

1. A Salesforce MCP server with query tools (for example the hosted `platform/sobject-reads` server): `getObjectSchema` to inspect objects and fields, `soqlQuery` to run a query, `find` to run a SOSL search.
2. The Salesforce CLI logged in to an org: `sf data query --query "<SOQL>" --target-org <alias>`. Add `--json` when you need to parse the result.
3. No connection: write the query and say it has not been run.

Never use a tool that creates, updates or deletes records. If the only connection available can write data, still run read queries only.

## Workflow

1. **Clarify the question.** Identify the object, the filters, the fields wanted and the expected size of the result. Ask one short question if the object or a filter is ambiguous. Do not guess a field.
2. **Discover the schema first.** With MCP, call `getObjectSchema` with no object name to find the object, then with the object name to get real field API names, relationships and record types. With the CLI, run `sf sobject describe --sobject <Object> --target-org <alias>`. Use only API names you have seen. Custom fields end in `__c` and custom relationships in `__r`.
3. **Write the query.** Follow `references/soql-checklist.md`. Select only the fields needed, filter with indexed fields where possible, bind values, and add `LIMIT` unless the user needs every row.
4. **Run a bounded version first.** Add `LIMIT 20` for the first run when the result size is unknown. For counts use `SELECT COUNT() FROM ...`.
5. **Report the result.** Show the query that was run, the row count and the key findings. Say when the result was truncated by a limit.
6. **Explain problems.** If the query fails or returns nothing, read the error, fix the query and run it again. Common causes: a misspelled field, a missing relationship name, a filter on a field the user cannot read, or a value in the wrong format.

## Rules

- Read-only. Never run DML, anonymous Apex or a tool that changes data.
- Do not select every field. Name the fields.
- Respect field-level security: if a field is not in the schema result, the user cannot read it. Say so; do not work around it.
- A query returns at most 50,000 rows in a transaction. For larger exports, suggest filtering by date ranges or using Bulk API.
- Treat query results as sensitive. Do not repeat personal data beyond what the user needs, and do not paste results into other tools.
- Do not use a negative filter (`!=`, `NOT IN`) or a leading wildcard (`LIKE '%x'`) without telling the user it may be slow on large objects.
- State assumptions, such as which record type or date range you used.

## Output format

1. The final SOQL in a code block.
2. A one-line explanation of what it returns.
3. The result summary (row count, notable values).
4. Any caveat: truncation, assumptions, or a faster alternative.
````

## Frequently asked questions

### Can this skill change or delete my Salesforce data?

No. The skill instructs the assistant to run read-only queries and never to use a write tool. Your connection should also be read-only, because instructions are not a security control. Use the hosted platform/sobject-reads server or a permission set without edit access.

### Do I need an MCP server, or does the Salesforce CLI work?

Either works. With an MCP server the assistant uses getObjectSchema and soqlQuery. With the CLI it uses sf sobject describe and sf data query. Without any connection the skill still writes queries but states that it has not run them.

### Why does it look at the schema before writing the query?

Assistants often guess field names and get them wrong, especially with custom fields such as Total_ARR__c. Reading the schema first means every field in the query was seen in your org.

### How many records can it return?

A SOQL query can return up to 50,000 records in one transaction, and the MCP soqlQuery tool follows that limit. For larger exports, the skill suggests narrowing the filter or using Bulk API.

### Does it work with assistants other than Claude?

Yes. Assistants that support the Agent Skills format can load the folder. For any other LLM, paste the contents of SKILL.md into the system prompt, and give it a Salesforce connection.

## Official sources

- https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql.htm
- https://developer.salesforce.com/docs/einstein/genai/guide/mcp.html
- https://agentskills.io/specification
