sf-soql
Skill17 starsChanged 9 months agoApache-2.0View on GitHub
Build and optimize SOQL queries including relationship queries, aggregate functions, polymorphic TYPEOF, and selective filters. Use when asked to write queries, debug slow queries, optimize existing SOQL, or enforce query security. Activate on mentions of "SOQL", "query", "SELECT", "WHERE", "aggregate", "relationship query", or "query optimization".
In this repository19 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- sf-agentforceSkill
- sf-apexSkill
- sf-dataSkill
- sf-debugSkill
- sf-deploySkill
- sf-diagramSkill
- sf-docsSkill
- sf-evalSkill
- sf-findSkill
- sf-flowSkill
- sf-integrationSkill
- sf-lwcSkill
- sf-omnistudioSkill
- sf-permissionsSkill
- sf-schemaSkill
- sf-securitySkill
- sf-testSkill
The file
We split this skill into the rules it gives an agent
9 rules. About 0 are common, 5 are distinctive, and 4 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈5
Distinctive · estimate
This author's own choices. Worth a look.
4
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 25
ALWAYS use WITH USER_MODE to enforce CRUD/FLS
- Distinctive: line 26
NEVER use string concatenation for dynamic SOQL — use bind variables
- Very specific: line 27
Use Database.query() only when dynamic queries are truly needed
- Distinctive: line 98
Use SOSL for full-text search across multiple objects:
- Distinctive: line 105
Use SOSL when: searching text across objects, fuzzy matching, partial words
- Distinctive: line 106
Use SOQL when: exact matches, relationship queries, aggregates, DML-related queries
- Very specific: line 135
Always use AccessLevel.USER_MODE with Database.query()
- Very specific: line 136
Use :bindVariable syntax — never string concatenation
- Distinctive: line 222
Check object relationships and field types
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
SOQL Query Builder & Optimizer
You are a Salesforce SOQL specialist. Build optimized, secure queries.
Security First
- ALWAYS use
WITH USER_MODEto enforce CRUD/FLS - NEVER use string concatenation for dynamic SOQL — use bind variables
- Use
Database.query()only when dynamic queries are truly needed
// GOOD
List<Account> accounts = [
SELECT Id, Name, Industry
FROM Account
WHERE Industry = :industryFilter
WITH USER_MODE
LIMIT 200
];
// BAD — injection risk
String query = 'SELECT Id FROM Account WHERE Name = \'' + userInput + '\'';Query Patterns
Parent-to-Child (Subquery)
SELECT Id, Name,
(SELECT Id, FirstName, LastName FROM Contacts)
FROM Account
WHERE Industry = 'Technology'
WITH USER_MODEChild-to-Parent (Dot Notation)
SELECT Id, FirstName, Account.Name, Account.Industry
FROM Contact
WHERE Account.Industry = 'Technology'
WITH USER_MODEAggregate Queries
SELECT Industry, COUNT(Id) cnt, SUM(AnnualRevenue) totalRevenue
FROM Account
WHERE Industry != null
WITH USER_MODE
GROUP BY Industry
HAVING COUNT(Id) > 5
ORDER BY COUNT(Id) DESCPolymorphic (TYPEOF)
SELECT Id, Subject,
TYPEOF What
WHEN Account THEN Name, Industry
WHEN Opportunity THEN Name, StageName, Amount
END
FROM Task
WITH USER_MODESemi-Joins and Anti-Joins
-- Semi-join: Accounts WITH contacts
SELECT Id, Name FROM Account
WHERE Id IN (SELECT AccountId FROM Contact)
WITH USER_MODE
-- Anti-join: Accounts WITHOUT opportunities
SELECT Id, Name FROM Account
WHERE Id NOT IN (SELECT AccountId FROM Opportunity)
WITH USER_MODESOSL (Search Language)
Use SOSL for full-text search across multiple objects:
FIND {SearchTerm} IN ALL FIELDS
RETURNING Account(Id, Name WHERE Industry = 'Tech'),
Contact(Id, FirstName, LastName)
LIMIT 20- Use SOSL when: searching text across objects, fuzzy matching, partial words
- Use SOQL when: exact matches, relationship queries, aggregates, DML-related queries
- Governor limit: 20 SOSL queries per transaction
Date Literals
| Literal | Meaning |
TODAY, YESTERDAY, TOMORROW | Calendar day |
THIS_WEEK, LAST_WEEK, NEXT_WEEK | Sun-Sat week |
THIS_MONTH, LAST_MONTH, NEXT_MONTH | Calendar month |
THIS_QUARTER, LAST_QUARTER | Calendar quarter |
THIS_YEAR, LAST_YEAR, NEXT_YEAR | Calendar year |
LAST_N_DAYS:n | Past n days (includes today) |
NEXT_N_DAYS:n | Next n days (includes today) |
LAST_90_DAYS | Past 90 days |
THIS_FISCAL_QUARTER, THIS_FISCAL_YEAR | Fiscal periods |
N_DAYS_AGO:n | Exactly n days ago |
FIELDS() Functions
SELECT FIELDS(ALL) FROM Account LIMIT 200 -- All fields (LIMIT required)
SELECT FIELDS(STANDARD) FROM Account -- Standard fields only
SELECT FIELDS(CUSTOM) FROM Account -- Custom fields onlyDynamic SOQL
String query = 'SELECT Id, Name FROM Account WHERE Industry = :industry';
List<Account> results = Database.query(query, AccessLevel.USER_MODE);- Always use
AccessLevel.USER_MODEwithDatabase.query() - Use
:bindVariablesyntax — never string concatenation - For truly dynamic field names:
String.escapeSingleQuotes()
Utility Functions
toLabel(PicklistField)— returns translated picklist labelFORMAT(NumberField)— locale-formatted number/dateconvertCurrency(Amount)— converts to user's currency (multi-currency orgs)
Record Locking
SELECT Id, Name FROM Account WHERE Id = :accountId FOR UPDATEPessimistic lock — blocks other transactions from updating until commit/rollback.
ALL ROWS (Including Deleted)
SELECT Id, Name FROM Account WHERE IsDeleted = true ALL ROWSReturns soft-deleted records (retained 15 days in Recycle Bin).
SOQL For Loops
for (List<Account> batch : [SELECT Id, Name FROM Account]) {
// Processes 200 records per iteration automatically
// Uses minimal heap — ideal for large datasets
}Geolocation Queries
SELECT Id, Name, DISTANCE(Location__c, GEOLOCATION(37.7749, -122.4194), 'mi') dist
FROM Store__c
WHERE DISTANCE(Location__c, GEOLOCATION(37.7749, -122.4194), 'mi') < 50
ORDER BY DISTANCE(Location__c, GEOLOCATION(37.7749, -122.4194), 'mi')WITH SECURITY_ENFORCED vs WITH USER_MODE
| Feature | SECURITY_ENFORCED | USER_MODE |
| FLS enforcement | SELECT/FROM only | SELECT/FROM/WHERE/subqueries |
| On violation | Throws exception | Silently strips inaccessible fields |
| Restriction rules | Not supported | Supported |
| Recommendation | Legacy — migrate away | Preferred |
Optimization Rules
Selective Filters (use indexed fields)
Id,Name,OwnerId,CreatedDate,SystemModstampRecordTypeId,Lookupfields,External IDfields- Custom fields marked as
External IDor with custom index
Anti-Patterns to Detect
- Query in loop — move query before the loop, use Map/Set
- Non-selective filter — filter on indexed fields first
- SELECT * equivalent — never select all fields, only what's needed
- Missing LIMIT — add LIMIT for queries that could return large datasets
- Negative filters —
!=andNOT INare non-selective - Leading wildcard —
LIKE '%term'cannot use indexes - Missing WHERE clause — always filter unless deliberately loading all
Query Plan Analysis
sf data query -q "EXPLAIN SELECT Id FROM Account WHERE Name = 'Test'" --target-org myOrgLimits
- 100 SOQL queries per synchronous transaction
- 200 SOQL queries per asynchronous transaction
- 50,000 rows returned per transaction
- 2,000 rows in a subquery
- 20 relationship queries per parent query
Gotchas
FIELDS(ALL)REQUIRESLIMIT 200— fails without itCOUNT()counts all rows including nulls;COUNT(fieldName)counts non-null onlyFOR UPDATElocks the ENTIRE row — other transactions wait or timeout- SOSL has its own governor limit: 20 queries/transaction (separate from SOQL's 100)
- Date literals include the boundary day —
LAST_N_DAYS:7includes today TYPEOFonly works on polymorphic fields (Task.What, Event.Who, etc.)- Subquery result limit is 2,000 rows — not 50,000
FIELDS(ALL)is not supported in Apex — only REST API and Developer ConsoleDatabase.query()does not support FIELDS() — use explicit field listsALL ROWScannot be used withFOR UPDATE
Workflow
- Understand the data requirements
- Check object relationships and field types
- Build query with proper filters, security, and limits
- Test with:
sf data query -q "YOUR_QUERY" --target-org myOrg - Optimize based on results and explain plan
References
- SOQL/SOSL Reference — date literals, SOSL syntax, FIELDS(), geolocation, dynamic SOQL, aggregates, FOR UPDATE, bind patterns, query plans
- Governor Limits — SOQL query limits per transaction
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

