SOQL Lib
The SOQL Lib provides functional constructs for SOQL queries in Apex.
Why SOQL Lib?
- Unit tests mocking - Mock SOQL results in unit tests without complex test data setup. Perfect for external objects and custom metadata.
- Advanced Caching System - Cache records in Apex transactions, Session Cache, or Org Cache for dramatic performance improvements.
- Dynamic SOQL Builder - No more string concatenation or massive selector classes with hundreds of methods.
- Built-in Security Controls - Enforce field-level security and sharing rules with WITH USER_MODE, WITH SYSTEM_MODE, and sharing settings by default.
- Result Transformation - Transform query results easily with built-in methods: toMap(), toIds(), toValuesOf(), and many more powerful utilities.
- Lightweight Selectors - Keep selector classes minimal and focused. Define business-specific queries inline where they're needed.
- Fluent API - Build SOQL queries dynamically with fluent API.
You can find more details about the SOQL Lib benefits here.
Quick Start
Inline Usage
apex
// via inline query
List<Account> accounts = SOQL.of(Account.SObjectType)
.with(Account.Id, Account.Name, Account.Industry)
.toList();
// via selector
List<Account> accounts = SOQL_Account.query()
.byType('Partner')
.byIndustry('IT')
.toList();apex
// cached query
Profile systemAdminProfile = (Profile) SOQLCache.of(Profile.SObjectType)
.with(Profile.Id, Profile.Name, Profile.UserType)
.whereEqual(Profile.Name, 'System Administrator')
.cacheInOrgCache()
.toObject()Selectors
apex
public inherited sharing class SOQL_Account extends SOQL implements SOQL.Selector {
@TestVisible
private static final String MOCK_ID = 'SOQL_Account';
public static SOQL_Account query() {
return new SOQL_Account();
}
private SOQL_Account() {
super(Account.SObjectType);
// default settings
with(Account.Id, Account.Name);
systemMode();
withoutSharing();
mockId(MOCK_ID);
}
public SOQL_Account byType(String type) {
whereAre(Filter.with(Account.Type).equal(type));
return this;
}
public SOQL_Account byIndustry(String industry) {
whereAre(Filter.with(Account.Industry).equal(industry));
return this;
}
public SOQL_Account byParentId(Id parentId) {
whereAre(Filter.with(Account.ParentId).equal(parentId));
return this;
}
public SOQL_Account byOwnerId(Id ownerId) {
whereAre(Filter.with(Account.OwnerId).equal(ownerId));
return this;
}
}Documentation
- Github Repository
- Documentation
- Trying out SOQL Lib - Sandbox Sessions - CloudBites TV
- SOQL Lib - Explanation
Installation
Choose the best installation option for you: