sf-soql-constraints
Skill15 starsChanged 7 months agoMITView on GitHub
Enforce SOQL/SOSL safety rules, selectivity, and governor limit compliance. Use when writing or reviewing ANY SOQL query, SOSL search, or database operations. Do NOT use for Apex structure, LWC, or Flow.
In this repository58 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- asideSkill
- checkpointSkill
- configure-sccSkill
- continuous-agent-loopSkill
- mcp-server-patternsSkill
- model-routeSkill
- prompt-optimizerSkill
- refactor-cleanSkill
- resume-sessionSkill
- save-sessionSkill
- search-firstSkill
- security-scanSkill
- sessionsSkill
- sf-2gp-security-reviewSkill
- sf-agentforce-developmentSkill
- sf-agentforce-mcp-actionsSkill
- sf-apex-async-patternsSkill
- sf-apex-best-practicesSkill
- sf-apex-constraintsSkill
- sf-apex-cursorSkill
- sf-apex-enterprise-patternsSkill
- sf-apex-testingSkill
- sf-api-designSkill
- sf-approval-processesSkill
- sf-aura-developmentSkill
- sf-build-fixSkill
- sf-cli-referenceSkill
- sf-data-modelingSkill
- sf-debuggingSkill
- sf-deployment-constraintsSkill
- sf-deploymentSkill
- sf-devops-ci-cdSkill
- sf-docs-lookupSkill
- sf-e2e-testingSkill
- sf-experience-cloudSkill
- sf-flow-developmentSkill
- sf-governor-limitsSkill
- sf-harness-auditSkill
- sf-helpSkill
- sf-integrationSkill
- sf-lwc-constraintsSkill
- sf-lwc-developmentSkill
- sf-lwc-testingSkill
- sf-metadata-managementSkill
- sf-platform-events-cdcSkill
- sf-quickstartSkill
- sf-security-constraintsSkill
- sf-securitySkill
- sf-soql-optimizationSkill
- sf-tdd-workflowSkill
- sf-testing-constraintsSkill
- sf-trigger-constraintsSkill
- sf-trigger-frameworksSkill
- sf-visualforce-developmentSkill
- strategic-compactSkill
- update-docsSkill
- update-platform-docsSkill
The file
We split this skill into the rules it gives an agent
6 rules. About 0 are common, 0 are distinctive, and 6 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
6
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 20
Never place SOQL or SOSL inside a loop: Every iteration consumes one of the per-transaction SOQL query budget (see …
- Very specific: line 26
Every WHERE clause must target an indexed field below the selectivity threshold (see @../_reference/SOQL_PATTERNS.md, Selectivity …
- Very specific: line 57
Never load all records just to count them: Use SELECT COUNT() FROM or aggregate queries instead of querying records and calling .size().
- Very specific: line 73
Always filter on indexed fields: Prefer Id, Name, OwnerId, RecordTypeId, CreatedDate, SystemModstamp, lookup/master-detail fields, or …
- Very specific: line 113
Do not memorize raw numbers -- always check @../_reference/GOVERNOR_LIMITS.md for the authoritative table.
- Very specific: line 121
Use Limits.getQueries() / Limits.getLimitQueries() to check remaining budget at runtime before issuing additional queries.
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 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 Constraints
When to Use
This skill auto-activates when writing, reviewing, or optimizing any SOQL query, SOSL search, or Apex database operation. It enforces query safety rules, selectivity requirements, and governor limit compliance for all database operations.
Hard rules for every SOQL query, SOSL search, and Apex database operation. Violations cause governor limit failures, security vulnerabilities, or production outages. See @../_reference/SOQL_PATTERNS.md for selectivity thresholds and @../_reference/GOVERNOR_LIMITS.md for per-transaction budgets.
Never Rules
- Never place SOQL or SOSL inside a loop. Every iteration consumes one
of the per-transaction SOQL query budget (see @../_reference/GOVERNOR_LIMITS.md). Query
once before the loop, store results in a
Map<Id, SObject>, then iterate. - Never write a non-selective query on objects with >200,000 rows. The query optimizer will full-table-scan and the query fails in trigger context. Every WHERE clause must target an indexed field below the selectivity threshold (see @../_reference/SOQL_PATTERNS.md, Selectivity Thresholds table).
- Never use
FIELDS(ALL)orFIELDS(CUSTOM)in triggers, service classes, or production paths. Select only the fields the calling code actually reads.FIELDS()directives are for exploration and debugging only. - Never hardcode Salesforce record IDs. IDs differ between orgs and
sandboxes. Use
Schema.SObjectType, Custom Metadata, or Custom Labels to resolve IDs at runtime. - Never omit
LIMITon unbounded queries. Any query that could return an unknown number of rows must include aLIMITclause to stay within the per-transaction row limit (see @../_reference/GOVERNOR_LIMITS.md). - Never concatenate user input into dynamic SOQL strings. This creates
SOQL injection vulnerabilities. Use bind variables (
:variable) for inline SOQL orDatabase.queryWithBinds()for dynamic SOQL. - Never use a leading wildcard in
LIKEfilters (LIKE '%term%'). Leading wildcards prevent index use and cause full table scans. Use SOSL (FIND) for full-text search instead. - Never use
!=orNOT INas the sole WHERE filter. These operators are non-optimizable and always produce table scans (see @../_reference/SOQL_PATTERNS.md, Optimizable vs Non-Optimizable Operators). - Never omit security enforcement on user-facing queries. Queries
triggered by user actions (LWC, Aura, VF, REST endpoints) must include
WITH USER_MODEor equivalent FLS/CRUD enforcement. - Never load all records just to count them. Use
SELECT COUNT() FROMor aggregate queries instead of querying records and calling.size().
Always Rules
- Always bulkify database operations. Collect IDs in a
Set<Id>, query once withWHERE Id IN :idSet, and store results in aMap<Id, SObject>. - Always use bind variables (
:variable) in inline SOQL. For dynamic SOQL, always useDatabase.queryWithBinds()with a bind map. - Always use
WITH USER_MODE(see @../_reference/API_VERSIONS.md for minimum version) on queries executed in user-facing contexts (LWC controllers, Aura controllers, VF controllers, REST resources). UseWITH SYSTEM_MODEonly for documented system processes (batch jobs, integrations) with explicit justification. - Always filter on indexed fields. Prefer
Id,Name,OwnerId,RecordTypeId,CreatedDate,SystemModstamp, lookup/master-detail fields, or External ID fields. See @../_reference/SOQL_PATTERNS.md, Standard Indexed Fields table for the full list. - Always add
LIMITwhen only one record is expected (LIMIT 1) or when displaying a bounded list. - Always use relationship queries (parent-to-child subqueries or child-to-parent dot notation) instead of separate queries when fetching related data. Subqueries do not count as separate SOQL queries.
- Always use SOSL instead of
LIKEfor text search across objects. SOSL uses the search index and is far more efficient thanLIKEon large-volume objects. - Always validate object and field names via
Schema.getGlobalDescribe()before building dynamic SOQL. Never trust external input for object or field names. - Always test triggers and services with 200 records (the standard trigger batch size) to validate bulk safety against governor limits.
Anti-Pattern Table
| Problem | Correct Pattern |
SOQL inside for loop | Query before loop, store in Map<Id, SObject> |
SELECT FIELDS(ALL) FROM Account in service class | SELECT Id, Name FROM Account -- explicit fields only |
WHERE Description LIKE '%keyword%' | FIND 'keyword' IN ALL FIELDS RETURNING Account(Id, Name) (SOSL) |
WHERE Custom_Field__c = 'value' on non-indexed field (LDV) | Add indexed field to WHERE, or request custom index |
String query = '...WHERE Name = \'' + input + '\'' | [SELECT Id FROM Account WHERE Name = :input] or Database.queryWithBinds() |
List<Account> all = [SELECT Id FROM Account]; Integer c = all.size(); | Integer c = [SELECT COUNT() FROM Account]; |
Hardcoded WHERE Id = '001xx000003DGXXX' | WHERE Id = :accountId with runtime-resolved variable |
No WITH USER_MODE on LWC controller query | Add WITH USER_MODE to enforce FLS + sharing |
| Separate queries for parent and child records | Use subquery: SELECT Id, (SELECT Id FROM Contacts) FROM Account |
WHERE Status__c != 'Closed' as only filter | Add a selective indexed filter: WHERE RecordTypeId = :rtId AND Status__c != 'Closed' |
Limit Budgets (Quick Reference)
Do not memorize raw numbers -- always check @../_reference/GOVERNOR_LIMITS.md for the authoritative table. Key constraint categories that shape every query decision:
- SOQL query limit per synchronous/asynchronous transaction (see @../_reference/GOVERNOR_LIMITS.md)
- Total rows returned per transaction (see @../_reference/GOVERNOR_LIMITS.md)
- Heap size synchronous/asynchronous (see @../_reference/GOVERNOR_LIMITS.md)
- CPU time synchronous/asynchronous (see @../_reference/GOVERNOR_LIMITS.md)
Use Limits.getQueries() / Limits.getLimitQueries() to check remaining
budget at runtime before issuing additional queries.
Related
- sf-soql-optimization -- Action skill for interactive query optimization, index strategy guidance, and Query Plan Tool usage.
- sf-apex-constraints -- Apex-level constraint rules (bulkification, DML patterns, CPU/heap management).
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.

