Skip to content

SOQL Lib

The SOQL Lib provides functional constructs for SOQL queries in Apex.

API version

License

GitHub Repo starsGitHub ReleaseCodecov

Documentation

You can find the full documentation at soql.beyondthecloud.dev.

What Does It Solve?

  • Too Many SOQL Queries: 101 - Too many queries in a single Apex transaction.
  • SOQL String Concatenation - Complex SOQL string concatenation that makes code hard to read.
  • Long-Running Unit Tests - Unit tests take a long time to execute due to complex data creation.
  • Low Code Coverage - Low code coverage due to complex conditions that are hard to cover.
  • Code Performance - Inefficient query result transformations scattered across the codebase.
  • Repeating Conditions - The same conditions and field sets repeated in many places.

Why SOQL Lib?

  • Unit Test 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 USER_MODE, SYSTEM_MODE, and sharing settings by default.
  • Result Transformation - Transform query results easily with built-in methods: toMap(), toIds(), toValuesOf(), and many more.
  • Lightweight Selectors - Keep selector classes minimal and focused. Define business-specific queries inline where they're needed.
  • Fluent API - Build SOQL queries dynamically with a 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

Installation

Choose the best installation option for you: