Skip to content

SEL_Base ​

Class · Group: Selectors

Learn it: Fast Start - Selectors and Selectors guide

apex
global abstract inherited sharing class SEL_Base implements IF_Queryable

Implements: 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

apex
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.

GroupMethodsWorked example
Find records by Id or by a fieldfindByField findByFields findById findByIdOrThrow findFirstByField findFirstByFieldsfindById, findByField
Read every record the selector coverscount exists getFirst getRandomItem toList toQueryLocatorExistence Checks, QueryLocator for Batch Apex
Set up your selectorgetFieldPaths getFields systemModeRequiredCustom Selector Classes, Switching a selector, or the whole framework, to system mode
Cache every readcacheScope cacheTimeToLiveSecondsCaching every read from a selector

Methods ​

MethodDescription
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 ​

apex
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

apex
public override UTIL_Cache.Scope cacheScope()
{
    return UTIL_Cache.Scope.IN_TRANSACTION;
}

cacheTimeToLiveSeconds ​

apex
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

apex
public override Integer cacheTimeToLiveSeconds()
{
    return 300;
}

count ​

apex
global Integer count()

Returns the count of all records matching the default query.

Returns Integer — Number of matching records

Example

apex
Integer result = instance.count();

exists ​

apex
global Boolean exists()

Returns true if any records match the default query.

Returns Boolean — True if at least one record matches

Example

apex
Boolean result = instance.exists();

findByField ​

apex
global List<SObject> findByField(SObjectField field, List<Object> values)

Finds records matching multiple values on a field.

Parameters

ParameterTypeDescription
fieldSObjectFieldThe SObjectField to filter on
valuesListThe values to match

Returns SObject — List of matching records, or empty list if no values provided

Example

apex
List<SObject> result = instance.findByField(Account.Name, new List<Object>{'a', 'b'});
apex
global List<SObject> findByField(SObjectField field, Object value)

Finds records matching a single field value.

Parameters

ParameterTypeDescription
fieldSObjectFieldThe SObjectField to filter on
valueObjectThe value to match

Returns SObject — List of matching records

Example

apex
List<SObject> result = instance.findByField(Account.Name, 'value');
apex
global List<SObject> findByField(SObjectField field, Set<Object> values)

Finds records matching multiple values on a field.

Parameters

ParameterTypeDescription
fieldSObjectFieldThe SObjectField to filter on
valuesSetThe values to match

Returns SObject — List of matching records, or empty list if no values provided

Example

apex
List<SObject> result = instance.findByField(Account.Name, new Set<Object>{'value'});

findByFields ​

apex
global List<SObject> findByFields(Map<SObjectField, Object> fieldValues)

Finds records matching multiple field-value pairs combined with AND logic.

Parameters

ParameterTypeDescription
fieldValuesMapMap of SObjectField tokens to their expected values

Returns SObject — List of matching records, or empty list if fieldValues is null or empty

Example

apex
List<SObject> records = new SEL_Foobar().findByFields
(
    new Map<SObjectField, Object>
    {
        Foobar__c.Text__c => 'Alpha',
        Foobar__c.Email__c => 'test@example.com'
    }
);

findById ​

apex
global SObject findById(Id recordId)

Finds a single record by its Id.

Parameters

ParameterTypeDescription
recordIdIdThe Id of the record to retrieve

Returns SObject — The matching record or null if not found

Example

apex
SObject result = instance.findById(recordId);
apex
global List<SObject> findById(Set<Id> recordIds)

Finds multiple records by their Ids.

Parameters

ParameterTypeDescription
recordIdsSetThe set of Ids to retrieve

Returns SObject — List of matching records, or empty list if no Ids provided

Example

apex
List<SObject> result = instance.findById(recordIds);

findByIdOrThrow ​

apex
global SObject findByIdOrThrow(Id recordId)

Finds a single record by its Id, throwing NotFoundException if not found.

Parameters

ParameterTypeDescription
recordIdIdThe Id of the record to retrieve

Returns SObject — The matching record

Throws

ExceptionDescription
UTIL_Exceptions.NotFoundExceptionIf no record exists with the given Id

Example

apex
Foobar__c record = (Foobar__c)new SEL_Foobar().findByIdOrThrow(recordId);
apex
global List<SObject> findByIdOrThrow(Set<Id> recordIds)

Finds multiple records by their Ids, throwing NotFoundException if any Id is missing.

Parameters

ParameterTypeDescription
recordIdsSetThe set of Ids to retrieve

Returns SObject — List of matching records

Throws

ExceptionDescription
UTIL_Exceptions.NotFoundExceptionIf any of the requested Ids were not found

Example

apex
List<Foobar__c> records = new SEL_Foobar().findByIdOrThrow(recordIds);

findFirstByField ​

apex
global SObject findFirstByField(SObjectField field, Object value)

Finds the first record matching a single field value, or null if not found.

Parameters

ParameterTypeDescription
fieldSObjectFieldThe SObjectField to filter on
valueObjectThe value to match

Returns SObject — The first matching record or null

Example

apex
SObject result = instance.findFirstByField(Account.Name, 'value');

findFirstByFields ​

apex
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

ParameterTypeDescription
fieldValuesMapMap of SObjectField tokens to their expected values

Returns SObject — The first matching record or null

Example

apex
SObject record = new SEL_Foobar().findFirstByFields
(
    new Map<SObjectField, Object>
    {
        Foobar__c.Text__c => 'Alpha',
        Foobar__c.Email__c => 'test@example.com'
    }
);

getFieldPaths ​

apex
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

apex
List<String> result = instance.getFieldPaths();

getFields ​

apex
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

apex
List<SObjectField> result = instance.getFields();

getFirst ​

apex
global SObject getFirst()

Executes the default query and returns the first record, or null.

Returns SObject — First matching record or null

Example

apex
SObject result = instance.getFirst();

getRandomItem ​

apex
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

apex
SObject result = instance.getRandomItem();

systemModeRequired ​

apex
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

apex
public inherited sharing class SEL_Cases extends SEL_Base
{
    public SEL_Cases()
    {
        super(Case.SObjectType);
    }
    public override Boolean systemModeRequired()
    {
        return true;
    }
}

toList ​

apex
global List<SObject> toList()

Executes the default query and returns all matching records.

Returns SObject — List of all records with default fields

Example

apex
List<SObject> result = instance.toList();

toQueryLocator ​

apex
global Database.QueryLocator toQueryLocator()

Returns a QueryLocator for the default query. Suitable for batch processing.

Returns Database.QueryLocator — QueryLocator for the default query

Example

apex
global class BatchProcessAccounts implements Database.Batchable
{
    global Database.QueryLocator start(Database.BatchableContext context)
    {
        return new SEL_Account().toQueryLocator();
    }
}

Constructors ​

ConstructorDescription
global SEL_Base(SObjectType objectType)Constructs a selector for the given SObjectType.

SEL_Base(SObjectType objectType) ​

apex
global SEL_Base(SObjectType objectType)

Constructs a selector for the given SObjectType.

Parameters

ParameterTypeDescription
objectTypeSObjectTypeThe SObjectType this selector queries

Example

apex
SEL_Account.SEL_Base instance = new SEL_Account.SEL_Base(Account.SObjectType);

Properties ​

PropertyDescription
global final List<String> fieldPathsCore field path strings for this selector, supporting relationship traversal syntax (e.g., 'Owner.Name').
global final List<SObjectField> fieldsCore SObjectField tokens for this selector.
global QRY_Builder.Builder queryReturns a new query builder pre-configured with this selector's SObjectType and all field sources.

fieldPaths ​

apex
global final List<String> fieldPaths

Type: List

Core field path strings for this selector, supporting relationship traversal syntax (e.g., 'Owner.Name'). Lazy-loaded from getCoreFieldPaths() on first access.

fields ​

apex
global final List<SObjectField> fields

Type: 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 ​

apex
global QRY_Builder.Builder query

Type: 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.