---
url: https://apexfluently.beyondthecloud.dev/libraries/soql-lib.md
description: >-
  Build type-safe SOQL queries with fluent API. Mock results in tests, cache
  queries, enforce security. FFLib Selector alternative.
---

# SOQL Lib

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

::: info Documentation
You can find the full documentation at [soql.beyondthecloud.dev](https://soql.beyondthecloud.dev).
:::

**SOQL Lib has been cloned over 11,000 times!**

## 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](https://soql.beyondthecloud.dev/soql/basic-features).

## Quick Start

### Inline Usage

```apex{2,7}
// 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{2}
// 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](https://github.com/beyond-the-cloud-dev/soql-lib)
* [Documentation](https://soql.beyondthecloud.dev/)
* [Trying out SOQL Lib - Sandbox Sessions - CloudBites TV](https://www.youtube.com/watch?t=2444\&v=pVtmmJSNnRA\&feature=youtu.be)
* [SOQL Lib - Explanation](https://blog.beyondthecloud.dev/blog/soql-lib)

## Installation

Choose the best installation option for you:

* [Install via Unlocked Package](https://soql.beyondthecloud.dev/installation#install-via-unlocked-package)
* [Install via Unmanaged Package](https://soql.beyondthecloud.dev/installation#install-via-unmanaged-package)
* [Deploy via Button](https://soql.beyondthecloud.dev/installation#deploy-via-button)
* [Copy and Deploy](https://soql.beyondthecloud.dev/installation#copy-and-deploy)
