Skip to content

SEL_Base

Class · Group: Selectors

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.

Since: 1.0

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


Properties

PropertyDescription
global final List fieldPathsCore field path strings for this selector, supporting relationship traversal syntax (e.g., 'Owner.Name').
global final List 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.

Methods

MethodDescription
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 findByField(SObjectField field, List<Object> values)Finds records matching multiple values on a field.
global List findByField(SObjectField field, Object value)Finds records matching a single field value.
global List findByField(SObjectField field, Set<Object> values)Finds records matching multiple values on a field.
global List 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 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 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 getFieldPaths()Returns core field paths as strings, supporting relationship traversal syntax (e.g., 'Owner.Name', 'Contact.Email').
global virtual List 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 SEL_Base(SObjectType objectType)Constructs a selector for the given SObjectType.
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 toList()Executes the default query and returns all matching records.
global Database.QueryLocator toQueryLocator()Returns a QueryLocator for the default query.

Property Details

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.

Since:

Example:

fields

apex
global final List<SObjectField> fields

Type: List

Core SObjectField tokens for this selector. Lazy-loaded from getCoreFields() on first access. Subscribers can inspect a selector's field configuration via this property.

Since:

Example:

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.

Since:

Example:


Method Details

SEL_Base

apex
global SEL_Base(SObjectType objectType)

Constructs a selector for the given SObjectType.

Parameters:

  • objectType (SObjectType) - The SObjectType this selector queries

Since: 1.0

Example:

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

count

apex
global Integer count()

Returns the count of all records matching the default query.

Returns: Integer - Number of matching records

Since: 1.0

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

Since: 1.0

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:

  • 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

Since: 1.0

Example:

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

findByField

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

Finds records matching a single field value.

Parameters:

Returns: SObject - List of matching records

Since: 1.0

Example:

apex
List<SObject> result = instance.findByField(Account.Name, 'value');

findByField

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

Finds records matching multiple values on a field.

Parameters:

  • 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

Since: 1.0

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:

  • 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

Since: 1.0

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:

  • recordId (Id) - The Id of the record to retrieve

Returns: SObject - The matching record or null if not found

Since: 1.0

Example:

apex
SObject result = instance.findById(recordId);

findById

apex
global List<SObject> findById(Set<Id> recordIds)

Finds multiple records by their Ids.

Parameters:

  • recordIds (Set) - The set of Ids to retrieve

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

Since: 1.0

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:

  • recordId (Id) - The Id of the record to retrieve

Returns: SObject - The matching record

Throws:

Since: 1.0

Example:

apex
Foobar__c record = (Foobar__c)new SEL_Foobar().findByIdOrThrow(recordId);

findByIdOrThrow

apex
global List<SObject> findByIdOrThrow(Set<Id> recordIds)

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

Parameters:

  • recordIds (Set) - The set of Ids to retrieve

Returns: SObject - List of matching records

Throws:

Since: 1.0

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:

Returns: SObject - The first matching record or null

Since: 1.0

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:

  • fieldValues (Map) - Map of SObjectField tokens to their expected values

Returns: SObject - The first matching record or null

Since: 1.0

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

Since: 1.0

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

Since: 1.0

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

Since: 1.0

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

Since: 1.0

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.

Subscriber-reachable 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.

Since: 1.0

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

Since: 1.0

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

Since: 1.0

Example:

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