Get started
Apex SOQL provides functional constructs for SOQL.
Examples
//SELECT Id FROM Account
List<Account> accounts = SOQL.of(Account.sObjectType).asList();
//SELECT Id, Name, Industry, Country FROM Account
List<Account> accounts = SOQL.of(Account.sObjectType)
.with(new List<sObjectField>{
Account.Id, Account.Name, Account.Industry, Account.Country
}).asList();
Benefits
- Additional level of abstraction - The selector layer is an additional level of abstraction that gives you the possibility to control the execution of SQOL.
- Mocking - Selector classes give a possibility to mock return values in unit tests.
- Mock external objects (__x) - External objects cannot be inserted in unit tests. You need to mock them.
- Mock custom metadata - Custom metadata cannot be inserted in unit tests unless the developer uses the Metadata API. Mock can be a solution.
- Control field-level security - The best practice is to execute SOQLs
WITH USER_MODE
to enforce field-level security and object permissions of the running user. The selector layer can applyWITH USER_MODE
by default to all of the queries, so the developer does not need to care about it. Developers can also addWITH SYSTEM_MODE
to all SOQLs from a specific selector. - Control sharings rules - The selector allows to execute of different methods in the same class in different sharing modes.
- Avoid duplicates - Generic SQOLs like
getById
, andgetByRecordType
can be stored in the selector class. - Default configuration - The selector class can provide default SOQL configuration like default fields, FLS settings, and sharing rules.
License notes
- For proper license management each repository should contain LICENSE file similar to this one.
- each original class should contain copyright mark: © Copyright 2022, Beyond The Cloud Dev Authors