The SOQL Lib provides functional constructs for SOQL queries in Apex.
For more details, please refer to the documentation.
// SELECT Id FROM Account
List<Account> accounts = SOQL.of(Account.SObjectType).toList();
// SELECT Id, Name, Industry FROM Account
List<Account> accounts = SOQL.of(Account.SObjectType)
.with(Account.Id, Account.Name, Account.Industry)
.toList();
public inherited sharing class SOQL_Contact extends SOQL implements SOQL.Selector {
public static SOQL_Contact query() {
return new SOQL_Contact();
}
private SOQL_Contact() {
super(Contact.SObjectType);
// default settings
with(Contact.Id, Contact.Name, Contact.AccountId)
.systemMode()
.withoutSharing();
}
public SOQL_Contact byRecordType(String rt) {
whereAre(Filter.recordType().equal(rt));
return this;
}
public SOQL_Contact byAccountId(Id accountId) {
whereAre(Filter.with(Contact.AccountId).equal(accountId));
return this;
}
}
public with sharing class ExampleController {
@AuraEnabled
public static List<Contact> getAccountContacts(Id accountId) {
return SOQL_Contact.query()
.byRecordType('Partner')
.byAccountId(accountId)
.with(Contact.Email)
.toList();
}
}

- Small Selector Classes - The selector class should be small and contains ONLY query base configuration (fields, sharing settings) and very generic methods (
byId
,byRecordType
). Why?- Huge classes are hard to manage.
- A lot of merge conflicts.
- Problems with methods naming.
- Build SOQL inline in a place of need - Business-specific SOQLs should be built inline via
SOQL
builder in a place of need.- Most of the queries on the project are case-specific and are not generic. There is no need to keep them in the Selector class.
- Build SOQL dynamically via builder - Developers should be able to adjust queries with specific fields, conditions, and other SOQL clauses.
- Do not spend time on selector methods naming - It can be difficult to find a proper name for a method that builds a query. The selector class contains methods like
selectByFieldAAndFieldBWithDescOrder
. It can be avoided by building SOQL inline in a place of need. - Control FLS and sharing settings - Selector should allow to control Field Level Security and sharing settings by simple methods like
.systemMode()
,.withSharing()
,.withoutSharing()
. - Auto binding - The selector should be able to bind variables dynamically without additional effort from the developer side.
- Mock results in Unit Tests - Selector should allow for mocking data in unit tests.
- For proper license management each repository should contain LICENSE file similar to this one.
- Each original class should contain copyright mark: Copyright (c) 2023 BeyondTheCloud.Dev