Skip to main content

SOQL

of

Conctructs an SOQL.

Signature

SOQL of(sObjectType ofObject)

Example

//SELECT Id FROM Account
SOQL.of(Account.sObjectType).asList();

//SELECT Id FROM Contact
SOQL.of(Contact.sObjectType).asList();

select

with fields

SELECT

SELECT statement that specifies the fields to query. The fieldList in the SELECT statement specifies the list of one or more fields, separated by commas, that you want to retrieve.

Signature

SOQL with(List<sObjectField> fields)

Example

//SELECT Id, Name, Industry FROM Account
SOQL.of(Account.sObjectType).with(List<sObjectField>{
Account.Id,
Account.Name,
Account.Industry
}).asList();

//SELECT Id, FirstName, LastName FROM Contact
SOQL.of(Contact.sObjectType).with(List<sObjectField>{
Contact.Id,
Contact.FirstName,
Contact.LastName
}).asList();

SELECT

Allows to add parent field to a query.

Signature

SOQL with(String relationshipPath, List<sObjectField> fields)

Example

//SELECT CreatedBy.Id, CreatedBy.Name FROM Account
SOQL.of(Account.sObjectType).with('CreatedBy', List<sObjectField>{
User.Id,
User.Name
}).asList();

//SELECT Account.Id, Account.Name FROM Contact
SOQL.of(Contact.sObjectType).with('Account', List<sObjectField>{
Account.Id,
Account.Name
}).asList();

count

COUNT()

COUNT() returns the number of rows that match the filtering conditions.

Signature

SOQL count()

Example

//SELECT COUNT() FROM Account
SOQL.of(Account.sObjectType).count().asInteger();

countAs

COUNT(fieldName)

COUNT(fieldName) returns the number of rows that match the filtering conditions and have a non-null value for fieldName.

Signature

countAs(sObjectField field, String alias)

Example

//SELECT COUNT(Name) names FROM Account
SOQL.of(Account.sObjectType).countAs(Account.Name, 'names').asAggregated();

with

Using Relationship Queries

Use SOQL to query several relationship types.

For more details check SOQL.SubQuery class.

Signature

SOQL with(SOQL.SubQuery subQuery)

Example

//SELECT Id, (SELECT Id, Name FROM Contacts) FROM Account
SOQL.of(Account.sObjectType)
.with(SOQL.SubQuery.of('Contacts')
.with(new List<sObjectField>{
Contact.Id,
Contact.Name
})
).asList();

scope

USING SCOPE

delegatedScope

Filter for records delegated to another user for action. For example, a query could filter for only delegated Task records.

Signature

SOQL delegatedScope()

Example

//SELECT Id FROM Task USING SCOPE DELEGATED
SOQL.of(Task.sObjectType).delegatedScope().asList();

mineScope

Filter for records owned by the user running the query.

Signature

SOQL mineScope()

Example

//SELECT Id FROM Account USING SCOPE MINE
SOQL.of(Account.sObjectType).mineScope().asList();

mineAndMyGroupsScope

Filter for records assigned to the user running the query and the user’s queues. If a user is assigned to a queue, the user can access records in the queue. This filter applies only to the ProcessInstanceWorkItem object.

Signature

SOQL mineAndMyGroupsScope()

Example

//SELECT Id FROM ProcessInstanceWorkItem USING SCOPE MINE_AND_MY_GROUPS
SOQL.of(ProcessInstanceWorkItem.sObjectType).mineAndMyGroupsScope().asList();

myTerritoryScope

Filter for records in the territory of the user running the query. This option is available if territory management is enabled for your organization.

Signature

SOQL myTerritoryScope()

Example

//SELECT Id FROM X USING SCOPE MY_TERRITORY

myTeamTerritoryScope

Filter for records in the territory of the team of the user running the query. This option is available if territory management is enabled for your organization.

Signature

SOQL myTeamTerritoryScope()

Example

//SELECT Id FROM X USING SCOPE MY_TEAM_TERRITORY

teamScope

Filter for records assigned to a team, such as an Account team.

Signature

SOQL teamScope()

Example

//SELECT Id FROM Account USING SCOPE TEAM
SOQL.of(Account.sObjectType).teamScope().asList();

whereAre

WHERE

The condition expression in a WHERE clause of a SOQL query includes one or more field expressions. You can specify multiple field expressions in a condition expression by using logical operators.

For more details check SOQL.FiltersGroup and SOQL.Filter

Signature

SOQL whereAre(FilterClause conditions)

Example

//SELECT Id FROM Account WHERE Id = :accountId OR Name = '%MyAccount%'
SOQL.of(Account.sObjectType)
.whereAre(SOQL.FiltersGroup
.add(SOQL.Filter.with(Account.Id).equal(accountId))
.add(SOQL.Filter.with(Account.Name).likeAny('MyAccount'))
.conditionLogic('1 OR 2')
).asList();

group by

GROUP BY

groupBy

You can use the GROUP BY option in a SOQL query to avoid iterating through individual query results. That is, you specify a group of records instead of processing many individual records.

Signature

SOQL groupBy(sObjectField field)

Example

//SELECT LeadSource FROM LEAD GROUP BY LeadSource
SOQL.of(Lead.sObjectType)
.with(new List<sObjectField>{
Lead.LeadSource
})
.groupBy(Lead.LeadSource)
.asAggregated();

groupByRollup

Signature

SOQL groupByRollup(sObjectField field)

Example


groupByCube

Signature

SOQL groupByCube(sObjectField field)

Example


order by

ORDER BY

orderBy

Use the optional ORDER BY in a SELECT statement of a SOQL query to control the order of the query results.

Signature

SOQL orderBy(sObjectField field)

Example

//SELECT Id FROM Account ORDER BY Name
SOQL.of(Account.sObjectType).orderBy(Account.Name).asList();

orderByRelated

Order SOQL query by parent field.

Signature

SOQL orderByRelated(String path, sObjectField field)

Example

//SELECT Id FROM Contact ORDER BY Account.Name
SOQL.of(Contact.sObjectType).orderByRelated('Account', Account.Name).asList();

sortDesc

Default order is ascending (ASC).

Signature

SOQL sortDesc()

Example

//SELECT Id FROM Account ORDER BY Name DESC
SOQL.of(Account.sObjectType).orderBy(Account.Name).sortDesc().asList();

nullsLast

By default, null values are sorted first (NULLS FIRST).

Signature

SOQL nullsLast()

Example

//SELECT Id FROM Account ORDER BY Name NULLS LAST
SOQL.of(Account.sObjectType).orderBy(Account.Industry).nullsLast().asList();

setLimit

LIMIT is an optional clause that can be added to a SELECT statement of a SOQL query to specify the maximum number of rows to return.

Signature

SOQL setLimit(Integer amount)

Example

//SELECT Id FROM Account LIMIT 100
SOQL.of(Account.sObjectType).setLimit(100).asList();

offset

When expecting many records in a query’s results, you can display the results in multiple pages by using the OFFSET clause on a SOQL query.

Signature

SOQL offset(Integer startingRow)

Example

//SELECT Id FROM Account OFFSET 10
SOQL.of(Account.sObjectType).setOffset(10).asList();

for

forReference

Use to notify Salesforce when a record is referenced from a custom interface, such as in a mobile application or from a custom page.

Signature

SOQL forReference()

Example

//SELECT Id FROM Contact FOR REFERENCE
SOQL.of(Contact.sObjectType).forReference().asList();

forView

Use to update objects with information about when they were last viewed.

Signature

SOQL forView()

Example

//SELECT Id FROM Contact FOR VIEW
SOQL.of(Contact.sObjectType).forView().asList();

forUpdate

Use to lock sObject records while they’re being updated in order to prevent race conditions and other thread safety problems.

Signature

SOQL forUpdate()

Example

//SELECT Id FROM Contact FOR UPDATE
SOQL.of(Contact.sObjectType).forUpdate().asList();

allRows

Signature

SOQL allRows()

Example

//SELECT Id FROM X ALL ROWS

fls

AccessLevel Class

By default AccessLevel is set as USER_MODE.

systemMode

Execution mode in which the the object and field-level permissions of the current user are ignored, and the record sharing rules are controlled by the class sharing keywords.

Signature

SOQL systemMode()

Example

SOQL.of(Account.sObjectType).systemMode().asList();
SOQL.of(Contact.sObjectType).userMode().asList();

sharing

Using the with sharing, without sharing, and inherited sharing Keywords

withSharing

Execute query with sharing

Signature

SOQL withSharing()

Example

SOQL.of(Account.sObjectType).withSharing().asList();
SOQL.of(Contact.sObjectType).withSharing().asList();

withoutSharing

Execute query without sharing

Signature

SOQL withoutSharing()

Example

SOQL.of(Account.sObjectType).withoutSharing().asList();
SOQL.of(Contact.sObjectType).withoutSharing().asList();

mocking

Signature

SOQL mocking(String queryIdentifier)

Example


preview

Signature

SOQL preview()

Example

SOQL.of(Account.sObjectType).preview().asList();
SOQL.of(Contact.sObjectType).preview().asList();

Query preview will be available in debug logs:

============ Query Preview ============
SELECT Name, AccountNumber, BillingCity, BillingCountry, BillingCountryCode
FROM Account
WHERE ((Id = :v1 OR Name LIKE :v2))
=======================================

============ Query Binding ============
{
"v2" : "%Test%",
"v1" : "0013V00000WNCw4QAH"
}
=======================================

result

asObject

Signature

sObject asObject()

Example


asList

Signature

List<sObject> asList()

Example


asAggregated

Signature

List<AggregateResult> asAggregated()

Example


asInteger

Signature

Integer asInteger()

Example