SEL_Base
Class · Group: Selectors
Learn it: Fast Start - Selectors and Selectors guide
global abstract inherited sharing class SEL_Base implements IF_QueryableImplements: IF_Queryable
Known Derived Types: SEL_ApiCall, SEL_ApiIssue, SEL_ContentVersion, SEL_EmailTemplate, SEL_Foobar, SEL_Group, SEL_OrgWideEmailAddress, SEL_PermissionSet, SEL_PermissionSetGroup, SEL_Profile, SEL_User, SEL_UserRole, IF_Queryable.count(), IF_Queryable.exists(), IF_Queryable.getFirst(), IF_Queryable.toList(), IF_Queryable.toQueryLocator()
Abstract base class for all selectors. Provides lazy-loaded field management and IF_Queryable implementation. Subclasses define their SObjectType and core fields; the base class handles query builder creation with all configured fields.
Example
global class SEL_Account extends SEL_Base
{
global SEL_Account()
{
super(Account.SObjectType);
}
global override List<SObjectField> getFields()
{
return new List<SObjectField> { Account.Id, Account.Name };
}
global override List<String> getFieldPaths()
{
return new List<String> { 'Owner.Name', 'CreatedBy.Email' };
}
global List<Account> findByIndustry(String industry)
{
return query.condition(Account.Industry).equals(industry).toList();
}
}See Also: IF_Queryable
Methods by task
Find a method by what you want to do. A group links to a worked example when a guide has one. Every method is described in the full list below.
| Group | Methods | Worked example |
|---|---|---|
| Find records by Id or by a field | findByField findByFields findById findByIdOrThrow findFirstByField findFirstByFields | findById, findByField |
| Read every record the selector covers | count exists getFirst getRandomItem toList toQueryLocator | Existence Checks, QueryLocator for Batch Apex |
| Set up your selector | getFieldPaths getFields systemModeRequired | Custom Selector Classes, Switching a selector, or the whole framework, to system mode |
| Cache every read | cacheScope cacheTimeToLiveSeconds | Caching every read from a selector |
Methods
| Method | Description |
|---|---|
| global virtual UTIL_Cache.Scope cacheScope() | Where this selector's reads may be cached. |
| global virtual Integer cacheTimeToLiveSeconds() | How long, in seconds, this selector's reads may be kept in Platform Cache. |
| global Integer count() | Returns the count of all records matching the default query. |
| global Boolean exists() | Returns true if any records match the default query. |
| global List<SObject> findByField(SObjectField field, List<Object> values) | Finds records matching multiple values on a field. |
| global List<SObject> findByField(SObjectField field, Object value) | Finds records matching a single field value. |
| global List<SObject> findByField(SObjectField field, Set<Object> values) | Finds records matching multiple values on a field. |
| global List<SObject> findByFields(Map<SObjectField, Object> fieldValues) | Finds records matching multiple field-value pairs combined with AND logic. |
| global SObject findById(Id recordId) | Finds a single record by its Id. |
| global List<SObject> findById(Set<Id> recordIds) | Finds multiple records by their Ids. |
| global SObject findByIdOrThrow(Id recordId) | Finds a single record by its Id, throwing NotFoundException if not found. |
| global List<SObject> findByIdOrThrow(Set<Id> recordIds) | Finds multiple records by their Ids, throwing NotFoundException if any Id is missing. |
| global SObject findFirstByField(SObjectField field, Object value) | Finds the first record matching a single field value, or null if not found. |
| global SObject findFirstByFields(Map<SObjectField, Object> fieldValues) | Finds the first record matching multiple field-value pairs combined with AND logic, or null if not found. |
| global virtual List<String> getFieldPaths() | Returns core field paths as strings, supporting relationship traversal syntax (e.g., 'Owner.Name', 'Contact.Email'). |
| global virtual List<SObjectField> getFields() | Returns the core SObjectField tokens always included in queries from this selector. |
| global SObject getFirst() | Executes the default query and returns the first record, or null. |
| global virtual SObject getRandomItem() | Returns a random record matching the default query, or null if none exist. |
| global virtual Boolean systemModeRequired() | Declares whether this selector's queries must run in AccessLevel.SYSTEM_MODE regardless of the UserModeQueries_Enabled feature flag. |
| global List<SObject> toList() | Executes the default query and returns all matching records. |
| global Database.QueryLocator toQueryLocator() | Returns a QueryLocator for the default query. |
cacheScope
global virtual UTIL_Cache.Scope cacheScope()Where this selector's reads may be cached. Override it to opt every read into caching; IN_TRANSACTION keeps results in memory for the current transaction only. The default caches nothing, unless cacheTimeToLiveSeconds() is overridden, in which case it is AUTO: the most durable storage that is safe for each read. User-mode reads are only ever kept for the current transaction. A system-mode selector caches only in methods that state their sharing with withSharing() or bypassSharing(). A method can change this for one query with query.withoutCache() or query.withCache(...). The scope and time-to-live are part of what this selector promises its callers about how old the rows may be.
Returns UTIL_Cache.Scope — The scope, or null for no caching
Example
public override UTIL_Cache.Scope cacheScope()
{
return UTIL_Cache.Scope.IN_TRANSACTION;
}cacheTimeToLiveSeconds
global virtual Integer cacheTimeToLiveSeconds()How long, in seconds, this selector's reads may be kept in Platform Cache. Ignored for reads kept in memory. Overriding only this method caches every read with the AUTO scope.
Returns Integer — Seconds, or null for the default of 900 seconds when a scope is set
Example
public override Integer cacheTimeToLiveSeconds()
{
return 300;
}count
global Integer count()Returns the count of all records matching the default query.
Returns Integer — Number of matching records
Example
Integer result = instance.count();exists
global Boolean exists()Returns true if any records match the default query.
Returns Boolean — True if at least one record matches
Example
Boolean result = instance.exists();findByField
global List<SObject> findByField(SObjectField field, List<Object> values)Finds records matching multiple values on a field.
Parameters
| Parameter | Type | Description |
|---|---|---|
field | SObjectField | The SObjectField to filter on |
values | List | The values to match |
Returns SObject — List of matching records, or empty list if no values provided
Example
List<SObject> result = instance.findByField(Account.Name, new List<Object>{'a', 'b'});global List<SObject> findByField(SObjectField field, Object value)Finds records matching a single field value.
Parameters
| Parameter | Type | Description |
|---|---|---|
field | SObjectField | The SObjectField to filter on |
value | Object | The value to match |
Returns SObject — List of matching records
Example
List<SObject> result = instance.findByField(Account.Name, 'value');global List<SObject> findByField(SObjectField field, Set<Object> values)Finds records matching multiple values on a field.
Parameters
| Parameter | Type | Description |
|---|---|---|
field | SObjectField | The SObjectField to filter on |
values | Set | The values to match |
Returns SObject — List of matching records, or empty list if no values provided
Example
List<SObject> result = instance.findByField(Account.Name, new Set<Object>{'value'});findByFields
global List<SObject> findByFields(Map<SObjectField, Object> fieldValues)Finds records matching multiple field-value pairs combined with AND logic.
Parameters
| Parameter | Type | Description |
|---|---|---|
fieldValues | Map | Map of SObjectField tokens to their expected values |
Returns SObject — List of matching records, or empty list if fieldValues is null or empty
Example
List<SObject> records = new SEL_Foobar().findByFields
(
new Map<SObjectField, Object>
{
Foobar__c.Text__c => 'Alpha',
Foobar__c.Email__c => 'test@example.com'
}
);findById
global SObject findById(Id recordId)Finds a single record by its Id.
Parameters
| Parameter | Type | Description |
|---|---|---|
recordId | Id | The Id of the record to retrieve |
Returns SObject — The matching record or null if not found
Example
SObject result = instance.findById(recordId);global List<SObject> findById(Set<Id> recordIds)Finds multiple records by their Ids.
Parameters
| Parameter | Type | Description |
|---|---|---|
recordIds | Set | The set of Ids to retrieve |
Returns SObject — List of matching records, or empty list if no Ids provided
Example
List<SObject> result = instance.findById(recordIds);findByIdOrThrow
global SObject findByIdOrThrow(Id recordId)Finds a single record by its Id, throwing NotFoundException if not found.
Parameters
| Parameter | Type | Description |
|---|---|---|
recordId | Id | The Id of the record to retrieve |
Returns SObject — The matching record
Throws
| Exception | Description |
|---|---|
| UTIL_Exceptions.NotFoundException | If no record exists with the given Id |
Example
Foobar__c record = (Foobar__c)new SEL_Foobar().findByIdOrThrow(recordId);global List<SObject> findByIdOrThrow(Set<Id> recordIds)Finds multiple records by their Ids, throwing NotFoundException if any Id is missing.
Parameters
| Parameter | Type | Description |
|---|---|---|
recordIds | Set | The set of Ids to retrieve |
Returns SObject — List of matching records
Throws
| Exception | Description |
|---|---|
| UTIL_Exceptions.NotFoundException | If any of the requested Ids were not found |
Example
List<Foobar__c> records = new SEL_Foobar().findByIdOrThrow(recordIds);findFirstByField
global SObject findFirstByField(SObjectField field, Object value)Finds the first record matching a single field value, or null if not found.
Parameters
| Parameter | Type | Description |
|---|---|---|
field | SObjectField | The SObjectField to filter on |
value | Object | The value to match |
Returns SObject — The first matching record or null
Example
SObject result = instance.findFirstByField(Account.Name, 'value');findFirstByFields
global SObject findFirstByFields(Map<SObjectField, Object> fieldValues)Finds the first record matching multiple field-value pairs combined with AND logic, or null if not found.
Parameters
| Parameter | Type | Description |
|---|---|---|
fieldValues | Map | Map of SObjectField tokens to their expected values |
Returns SObject — The first matching record or null
Example
SObject record = new SEL_Foobar().findFirstByFields
(
new Map<SObjectField, Object>
{
Foobar__c.Text__c => 'Alpha',
Foobar__c.Email__c => 'test@example.com'
}
);getFieldPaths
global virtual List<String> getFieldPaths()Returns core field paths as strings, supporting relationship traversal syntax (e.g., 'Owner.Name', 'Contact.Email'). Override to include relationship fields in the default query. Returns an empty list by default.
Returns String — List of field path strings
Example
List<String> result = instance.getFieldPaths();getFields
global virtual List<SObjectField> getFields()Returns the core SObjectField tokens always included in queries from this selector. Override to define the default field set. Returns an empty list by default.
Returns SObjectField — List of core SObjectFields
Example
List<SObjectField> result = instance.getFields();getFirst
global SObject getFirst()Executes the default query and returns the first record, or null.
Returns SObject — First matching record or null
Example
SObject result = instance.getFirst();getRandomItem
global virtual SObject getRandomItem()Returns a random record matching the default query, or null if none exist.
Returns SObject — A random matching record or null
Example
SObject result = instance.getRandomItem();systemModeRequired
global virtual Boolean systemModeRequired()Declares whether this selector's queries must run in AccessLevel.SYSTEM_MODE regardless of the UserModeQueries_Enabled feature flag. Framework-internal selectors that read CMDT, framework-owned sObjects, or system-schema tables override this to return true so they continue working when the running user lacks FLS/CRUD on those objects by design.
Your own selectors should leave this at the default (false) so they inherit the flag-driven default — secure-by-default when the flag is enabled.
Returns Boolean — true when this selector's queries must run in SYSTEM_MODE.
Example
public inherited sharing class SEL_Cases extends SEL_Base
{
public SEL_Cases()
{
super(Case.SObjectType);
}
public override Boolean systemModeRequired()
{
return true;
}
}toList
global List<SObject> toList()Executes the default query and returns all matching records.
Returns SObject — List of all records with default fields
Example
List<SObject> result = instance.toList();toQueryLocator
global Database.QueryLocator toQueryLocator()Returns a QueryLocator for the default query. Suitable for batch processing.
Returns Database.QueryLocator — QueryLocator for the default query
Example
global class BatchProcessAccounts implements Database.Batchable
{
global Database.QueryLocator start(Database.BatchableContext context)
{
return new SEL_Account().toQueryLocator();
}
}Constructors
| Constructor | Description |
|---|---|
| global SEL_Base(SObjectType objectType) | Constructs a selector for the given SObjectType. |
SEL_Base(SObjectType objectType)
global SEL_Base(SObjectType objectType)Constructs a selector for the given SObjectType.
Parameters
| Parameter | Type | Description |
|---|---|---|
objectType | SObjectType | The SObjectType this selector queries |
Example
SEL_Account.SEL_Base instance = new SEL_Account.SEL_Base(Account.SObjectType);Properties
| Property | Description |
|---|---|
| global final List<String> fieldPaths | Core field path strings for this selector, supporting relationship traversal syntax (e.g., 'Owner.Name'). |
| global final List<SObjectField> fields | Core SObjectField tokens for this selector. |
| global QRY_Builder.Builder query | Returns a new query builder pre-configured with this selector's SObjectType and all field sources. |
fieldPaths
global final List<String> fieldPathsType: List
Core field path strings for this selector, supporting relationship traversal syntax (e.g., 'Owner.Name'). Lazy-loaded from getCoreFieldPaths() on first access.
fields
global final List<SObjectField> fieldsType: List
Core SObjectField tokens for this selector. Lazy-loaded from getCoreFields() on first access. You can inspect a selector's field configuration via this property.
query
global QRY_Builder.Builder queryType: QRY_Builder.Builder
Returns a new query builder pre-configured with this selector's SObjectType and all field sources. Each access creates a fresh builder instance to prevent state leaking between queries. When systemModeRequired() returns true, the builder has systemModeInternal() applied so framework-internal reads (CMDT, framework-owned sObjects) run in SYSTEM_MODE without polluting the BypassEvent audit trail: a static systemModeRequired() design choice is not a runtime bypass. Selectors that leave systemModeRequired() at the default (false) inherit the UserModeQueries_Enabled flag-driven default. When cacheScope() returns a scope, the builder also has withCache(cacheScope(), cacheTimeToLiveSeconds()) applied, so every read from this selector is cached.