-
Notifications
You must be signed in to change notification settings - Fork 0
The Soql Class
The Soql class is designed to facilitate the construction and execution of SOQL queries within the Salesforce platform.
This class abstracts direct inline SOQL queries, promoting testability by allowing query methods to be mocked using the MockSoql class. The class allows developers to create and execute fully customizable queries, with methods that map directly to Salesforce's underlying Database query methods.
Use this class in place of inline SOQL to pave the way for faster, more scalable unit tests that run independent of the Salesforce database.
Soql objects cannot be directly constructed via the new keyword. Instead, use the DatabaseLayer.Soql.newQuery(SObjectType fromSObject) method.
The object uses the Soql.Builder class to allow for flexible query construction. Once your query is built, call toSoql() to build the query as a Soql object:
Soql query = DatabaseLayer.Soql
?.newQuery(Account.SObjectType)
?.addSelect(Account.Name)
?.setRowLimit(200)
?.toSoql();In @IsTest context, the DatabaseLayer.useMocks() method ensures that an instance of the MockSoql class will be returned for each subsuquent newQuery call:
DatabaseLayer.useMocks();
MockSoql query = (MockSoql) DatabaseLayer.Soql.newQuery(Account.SObjectType);
Assert.isInstanceOfType(query, MockSoql.class, 'Not a mock');Use Soql.Builder class's various builder methods to construct a SOQL query. Each of these methods returns a Soql.Builder instance, which can be used to support fluent query constrution:
Soql accountQuery = DatabaseLayer.Soql.newQuery(Account.SObjectType)
?.addSelect(Account.Name)
?.addWhere(Account.Type, Soql.NOT_EQUALS, 'Internal')
?.withSecurityEnforced()
?.addOrderBy(Account.CreatedDate, Soql.SortDirection.DESCENDING)
?.setRowLimit(200)
?.toSoql();
List<Account> accounts = accountQuery?.query();The Soql class contains several methods which provide parity with the query methods found in the standard Database class:
⚠️ Note: TheSoqlclass includes several methods used to build SOQL queries. These methods are inherited from theSoql.Builderclass. TheSoql.InnerQueryandSoql.Subqueryalso extendSoql.Builder, and share the same query-building methods.All query-building methods are documented here:
Soql.Builder
Performs aggregate queries and returns results as a list of Soql.AggregateResult objects. This type wraps the Schema.AggregateResult objects to provide mockable results in tests.
List<Soql.AggregateResult> aggregateQuery()
Executes a count query, which should only contain aggregation functions like COUNT(). Returns the count of records that match the query.
Integer countQuery()
Retrieves a Soql.Cursor object, which decorates a Database.Cursor object.
Soql.Cursor getCursor()
Retrieves a Soql.QueryLocator object, which decorates a Database.QueryLocator object.
Soql.QueryLocator getQueryLocator()
Executes the query and returns the results as a list of SObject. Alternatively, this method can return results as a specific returnType, ie., for aggregate queries.
List<SObject> query()Object query(Type returnType)
Fetches the first result of the query or returns null if no results are found. This method is useful for cases where only a single result is expected.
SObject queryFirst()
Assigns an identifier to the query. Callers can use this identifier to distinguish queries from one another, for example in mocks.
Soql setQueryIdentifier(String identifier)
- Generating Test Records
- Dml
- Soql
- Cmdt
- Plugins
- DatabaseLayer
- Dml
- MockDml
- MockRecord
- Cmdt
- MockCmdt
- MockSoql
-
Soql
- Soql.AggregateResult
- Soql.Aggregation
- Soql.Binder
- Soql.Builder
- Soql.Condition
- Soql.ConditionalLogic
- Soql.Criteria
- Soql.Cursor
- Soql.Function
- Soql.InnerQuery
- Soql.InvalidParameterValueException
- Soql.LogicType
- Soql.NullOrder
- Soql.Operation
- Soql.Operator
- Soql.ParentField
- Soql.PreAndPostProcessor
- Soql.QueryLocator
- Soql.Request
- Soql.Scope
- Soql.Selectable
- Soql.SortDirection
- Soql.SortOrder
- Soql.Subquery
- Soql.TypeOf
- Soql.Usage
- Soql.WhenClause