Skip to main content

SELECT

Specify fields that will be retrieved via query.

Fields

You are able to add a default fields to selector class. More fields can be added in a place of usage.

SELECT Id, Name, BillingCity, BillingState, BillingStreet, BillingCountry
FROM Account
public inherited sharing class AccountSelector implements SOQL.Selector {

public static SOQL query() {
return SOQL.of(Account.SObjectType)
.with(new List<SObjectField>{ //default fields
Account.Id,
Account.Name
});
}
}

public with sharing class MyController {

public static List<Account> getAccounts() {
return AccountSelector.query()
.with(new List<SObjectField>{
Account.BillingCity,
Account.BillingState,
Account.BillingStreet,
Account.BillingCountry
}).toList();
}
}

Parent Fields

Specify relationship name and pass parent object fields.

SELECT Id, Name, CreatedBy.Id, CreatedBy.Name
FROM Account
public inherited sharing class AccountSelector implements SOQL.Selector {

public static SOQL query() {
return SOQL.of(Account.SObjectType) //default fields
.with(new List<SObjectField>{
Account.Id,
Account.Name
});
}
}

public with sharing class MyController {

public static List<Account> getAccountsWithCreatedBy() {
return AccountSelector.query()
.with('CreatedBy', new List<SObjectField>{
User.Id,
User.Name
}).toList();
}
}

Count

SELECT COUNT() FROM Account

SELECT COUNT(Name) names FROM Account
public inherited sharing class AccountSelector implements SOQL.Selector {

public static SOQL query() {
return SOQL.of(Account.SObjectType);
}
}

public with sharing class MyController {

public static Integer getAccountAmount() {
return AccountSelector.query().count().toInteger();
}

public static Integer getUniqueAccountNameAmount() {
return AccountSelector.query().count(Account.Name, 'names').toAggregated()[0].names;
}
}