forcedotcom/sf-skills / plugins/builder/salesforce-development/skills/platform-soql-query
platform-soql-query
VerifiedUse when the user needs SOQL/SOSL authoring or optimization: natural-language-to-query generation, relationship queries, aggregates, query-plan/selectivity analysis, and performance or safety improvements. TRIGGER when the user writes, optimizes, or debugs SOQL/SOSL, touches .soql files, or asks about relationship queries, aggregates, or query performance. DO NOT TRIGGER for bulk data operations (use platform-data-manage), Apex DML logic (use platform-apex-generate), or report/dashboard queries.
| File | Kind | Size |
|---|---|---|
| README.md | markdown | 1.7 KB |
| SKILL.md | skill md | 6.3 KB |
| assets/aggregate-queries.soql | other | 9.4 KB |
| assets/basic-queries.soql | other | 7.4 KB |
| assets/bulkified-query-pattern.cls | other | 10.7 KB |
| assets/optimization-patterns.soql | other | 10.9 KB |
| assets/relationship-queries.soql | other | 7.5 KB |
| assets/selector-class.cls | other | 8.4 KB |
| references/anti-patterns.md | markdown | 8.7 KB |
| references/cli-commands.md | markdown | 7.2 KB |
| references/field-coverage-rules.md | markdown | 14.4 KB |
| references/query-optimization.md | markdown | 3.2 KB |
| references/selector-patterns.md | markdown | 13.7 KB |
| references/soql-reference.md | markdown | 6.4 KB |
| references/soql-syntax-reference.md | markdown | 4.5 KB |
| scripts/post-tool-validate.py | script | 11.9 KB |
assets/selector-class.cls
View on GitHub/**
* ${OBJECT_NAME}Selector - Centralized SOQL queries for ${OBJECT_NAME}
*
* This selector class follows the Selector Layer pattern to:
* - Centralize all ${OBJECT_NAME} queries in one place
* - Enforce Field-Level Security (FLS) by default
* - Support query mocking in unit tests
* - Provide consistent field selection across the codebase
*
* @see https://blog.beyondthecloud.dev/blog/why-do-you-need-selector-layer
* @see https://www.jamessimone.net/blog/joys-of-apex/repository-pattern/
*/
public inherited sharing class ${OBJECT_NAME}Selector {
// ═══════════════════════════════════════════════════════════════════
// FIELD DEFINITIONS
// Centralize field lists to make updates easy
// ═══════════════════════════════════════════════════════════════════
/**
* Standard fields queried in most operations
*/
private static final List<SObjectField> STANDARD_FIELDS = new List<SObjectField>{
${OBJECT_NAME}.Id,
${OBJECT_NAME}.Name,
${OBJECT_NAME}.OwnerId,
${OBJECT_NAME}.CreatedDate,
${OBJECT_NAME}.LastModifiedDate
// Add commonly needed fields here
};
/**
* Extended fields for detail views
*/
private static final List<SObjectField> DETAIL_FIELDS = new List<SObjectField>{
${OBJECT_NAME}.Id,
${OBJECT_NAME}.Name,
${OBJECT_NAME}.OwnerId,
${OBJECT_NAME}.CreatedDate,
${OBJECT_NAME}.LastModifiedDate
// Add detail-specific fields here
};
// ═══════════════════════════════════════════════════════════════════
// MOCKING SUPPORT (for unit tests)
// ═══════════════════════════════════════════════════════════════════
@TestVisible
private static List<${OBJECT_NAME}> mockResults;
@TestVisible
private static void setMockResults(List<${OBJECT_NAME}> records) {
mockResults = records;
}
@TestVisible
private static void clearMockResults() {
mockResults = null;
}
private static Boolean useMock() {
return Test.isRunningTest() && mockResults != null;
}
// ═══════════════════════════════════════════════════════════════════
// QUERY METHODS
// ═══════════════════════════════════════════════════════════════════
/**
* Query records by their IDs
*
* @param recordIds Set of record IDs to query
* @return List of matching records with standard fields
*/
public static List<${OBJECT_NAME}> byIds(Set<Id> recordIds) {
if (recordIds == null || recordIds.isEmpty()) {
return new List<${OBJECT_NAME}>();
}
if (useMock()) {
return mockResults;
}
return [
SELECT Id, Name, OwnerId, CreatedDate, LastModifiedDate
FROM ${OBJECT_NAME}
WHERE Id IN :recordIds
WITH SECURITY_ENFORCED
];
}
/**
* Query records by their IDs, returning a Map for O(1) lookups
*
* @param recordIds Set of record IDs to query
* @return Map of Id to record
*/
public static Map<Id, ${OBJECT_NAME}> byIdsAsMap(Set<Id> recordIds) {
return new Map<Id, ${OBJECT_NAME}>(byIds(recordIds));
}
/**
* Query records by Owner
*
* @param ownerId The owner's User Id
* @return List of records owned by the specified user
*/
public static List<${OBJECT_NAME}> byOwnerId(Id ownerId) {
if (ownerId == null) {
return new List<${OBJECT_NAME}>();
}
if (useMock()) {
return mockResults;
}
return [
SELECT Id, Name, OwnerId, CreatedDate, LastModifiedDate
FROM ${OBJECT_NAME}
WHERE OwnerId = :ownerId
WITH SECURITY_ENFORCED
ORDER BY LastModifiedDate DESC
LIMIT 1000
];
}
/**
* Query records created within a date range
*
* @param startDate Start of date range (inclusive)
* @param endDate End of date range (inclusive)
* @return List of records created within the range
*/
public static List<${OBJECT_NAME}> byCreatedDateRange(Date startDate, Date endDate) {
if (startDate == null || endDate == null) {
return new List<${OBJECT_NAME}>();
}
if (useMock()) {
return mockResults;
}
return [
SELECT Id, Name, OwnerId, CreatedDate, LastModifiedDate
FROM ${OBJECT_NAME}
WHERE CreatedDate >= :startDate
AND CreatedDate <= :endDate
WITH SECURITY_ENFORCED
ORDER BY CreatedDate DESC
LIMIT 10000
];
}
// ═══════════════════════════════════════════════════════════════════
// RELATIONSHIP QUERIES
// ═══════════════════════════════════════════════════════════════════
/**
* Query records with related child records
* Customize the subquery for your object relationships
*
* @param recordIds Set of parent record IDs
* @return List of parent records with child records populated
*/
public static List<${OBJECT_NAME}> withChildRecordsByIds(Set<Id> recordIds) {
if (recordIds == null || recordIds.isEmpty()) {
return new List<${OBJECT_NAME}>();
}
if (useMock()) {
return mockResults;
}
return [
SELECT Id, Name, OwnerId,
(SELECT Id, Name
FROM ChildRelationshipName__r // Replace with actual relationship
WHERE IsActive__c = true
LIMIT 50)
FROM ${OBJECT_NAME}
WHERE Id IN :recordIds
WITH SECURITY_ENFORCED
];
}
// ═══════════════════════════════════════════════════════════════════
// AGGREGATE QUERIES
// ═══════════════════════════════════════════════════════════════════
/**
* Count records by a grouping field
*
* @return List of AggregateResult with grouping and count
*/
public static List<AggregateResult> countByOwner() {
return [
SELECT OwnerId, COUNT(Id) recordCount
FROM ${OBJECT_NAME}
GROUP BY OwnerId
HAVING COUNT(Id) > 0
];
}
// ═══════════════════════════════════════════════════════════════════
// EXISTENCE CHECK
// ═══════════════════════════════════════════════════════════════════
/**
* Check if a record exists by ID (efficient single record lookup)
*
* @param recordId The record ID to check
* @return true if the record exists
*/
public static Boolean existsById(Id recordId) {
if (recordId == null) {
return false;
}
List<${OBJECT_NAME}> results = [
SELECT Id
FROM ${OBJECT_NAME}
WHERE Id = :recordId
WITH SECURITY_ENFORCED
LIMIT 1
];
return !results.isEmpty();
}
}
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack." | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |