Skip to content

Optimizing SOQL queries with a selector framework.

Notifications You must be signed in to change notification settings

ammyt/apex-selectors

Repository files navigation

apex-selectors

Optimizing SOQL queries with a selector framework.

This is by no means a perfect tool, rather just something small I developed for myself to make SOQL queries a bit more maintainable. You are free to use this / make it better.

Implementation

public class AccountSelector extends AbstractSelector {
    // Constructor is private to control instantiation
    private AccountSelector() {
        super();
    }

    private AccountSelector(String fields) {
        super(fields);
    }

    protected override Schema.SObjectType getSObjectType() {
        return Schema.getGlobalDescribe().get('Account');
    }

    // Static factory method to instantiate AccountSelector
    public static AccountSelector fetch() {
        return new AccountSelector();
    }

    public static AccountSelector fetch(String fields) {
        return new AccountSelector(fields);
    }
}

Usage

List<Account> results = (List<Account>) AccountSelector.fetch().executeQuery();

List<Account> results = (List<Account>) AccountSelector.fetch()
            .filter('Id = :accountId')
            .bindParams(new Map<String, Object> {
                'accountId' => accountId
            })
            .executeQuery();

List<Account> results = (List<Account>) AccountSelector.fetch()
            .orderBy('Name')
            .executeQuery();

Account result = (Account) AccountSelector.fetch().setLimit(1).executeQuery();

Account result = (Account) AccountSelector.fetch('Name, Parent.Name').setLimit(1).executeQuery();

About

Optimizing SOQL queries with a selector framework.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published