RESULT
For more details check SOQLEvaluator API - RESULT.
toId
Apex
Traditional Approach
List<Account> accounts = [SELECT Id FROM Account WITH USER_MODE LIMIT 1];
Id accountId = accounts.isEmpty() ? null : accounts[0].Id;
SOQL Lib
SOQLEvaluator Approach
Id accountId = SOQLEvaluator.of([
SELECT Id
FROM Account
WITH USER_MODE
LIMIT 1
]).toId();
toIds
Apex
Traditional Approach
Set<Id> accountIds = new Set<Id>();
for (Account acc : [SELECT Id FROM Account WHERE Industry = 'Technology' WITH USER_MODE]) {
accountIds.add(acc.Id);
}
SOQL Lib
SOQLEvaluator Approach
Set<Id> accountIds = SOQLEvaluator.of([
SELECT Id
FROM Account
WHERE Industry = 'Technology'
WITH USER_MODE
]).toIds();
toIdsOf
Apex
Traditional Approach
Set<Id> ownerIds = new Set<Id>();
for (Account acc : [SELECT OwnerId FROM Account WHERE Industry = 'Technology' WITH USER_MODE]) {
if (acc.OwnerId != null) {
ownerIds.add(acc.OwnerId);
}
}
SOQL Lib
SOQLEvaluator Approach
Set<Id> ownerIds = SOQLEvaluator.of([
SELECT OwnerId
FROM Account
WHERE Industry = 'Technology'
WITH USER_MODE
]).toIdsOf(Account.OwnerId);
toIdsOf related field
Apex
Set<Id> parentAccountIds = new Set<Id>();
for (Account acc : [SELECT Parent.Id FROM Account WHERE Industry = 'Technology' WITH USER_MODE]) {
if (acc.Parent?.Id != null) {
parentAccountIds.add(acc.Parent.Id);
}
}
SOQL Lib
Set<Id> parentAccountIds = SOQLEvaluator.of([
SELECT Parent.Id
FROM Account
WHERE Industry = 'Technology'
WITH USER_MODE
]).toIdsOf('Parent', Account.Id);
doExist
Apex
List<Account> accounts = [
SELECT Id
FROM Account
WHERE NumberOfEmployees > 100
WITH USER_MODE
LIMIT 1
];
Boolean hasAccountsWithMoreThan100Employees = !accounts.isEmpty();
SOQL Lib
Boolean hasAccountsWithMoreThan100Employees = SOQLEvaluator.of([
SELECT Id
FROM Account
WHERE NumberOfEmployees > 100
WITH USER_MODE
]).doExist();
toValueOf
Apex
List<Account> accounts = [SELECT Name FROM Account WHERE Id = '1234' WITH USER_MODE LIMIT 1];
String accountName = accounts.isEmpty() ? null : accounts[0].Name;
SOQL Lib
String accountName = (String) SOQLEvaluator.of([
SELECT Name
FROM Account
WHERE Id = '1234'
WITH USER_MODE
LIMIT 1
]).toValueOf(Account.Name);
toValuesOf
Apex
Set<String> accountNames = new Set<String>();
for (Account acc : [SELECT Name FROM Account WITH USER_MODE]) {
if (acc.Name != null) {
accountNames.add(acc.Name);
}
}
SOQL Lib
Set<String> accountNames = SOQLEvaluator.of([
SELECT Name
FROM Account
WITH USER_MODE
]).toValuesOf(Account.Name);
toObject
Apex
List<Account> accounts = [
SELECT Id, Name
FROM Account
WHERE Id = '1234'
WITH USER_MODE
LIMIT 1
];
Account account = accounts.isEmpty() ? null : accounts[0];
SOQL Lib
Account account = (Account) SOQLEvaluator.of([
SELECT Id, Name
FROM Account
WHERE Id = '1234'
WITH USER_MODE
LIMIT 1
]).toObject();
toList
Apex
List<Account> accounts = [SELECT Id, Name FROM Account WITH USER_MODE];
SOQL Lib
List<Account> accounts = SOQLEvaluator.of([
SELECT Id, Name
FROM Account
WITH USER_MODE
]).toList();
toMap
Apex
Map<Id, Account> idToAccount = new Map<Id, Account>([SELECT Id FROM Account WITH USER_MODE]);
SOQL Lib
Map<Id, Account> idToAccount = (Map<Id, Account>) SOQLEvaluator.of([
SELECT Id
FROM Account
WITH USER_MODE
]).toMap();
toMap with custom key
Apex
Map<String, Account> nameToAccount = new Map<String, Account>();
for (Account acc : [SELECT Id, Name FROM Account WITH USER_MODE]) {
nameToAccount.put(acc.Name, acc);
}
SOQL Lib
Map<String, Account> nameToAccount = (Map<String, Account>) SOQLEvaluator.of([
SELECT Id, Name
FROM Account
WITH USER_MODE
]).toMap(Account.Name);
toMap with custom relationship key
Apex
Map<String, Account> createdByEmailToAccount = new Map<String, Account>();
for (Account acc : [SELECT Id, CreatedBy.Email FROM Account WITH USER_MODE]) {
if (acc.CreatedBy?.Email != null) {
createdByEmailToAccount.put(acc.CreatedBy.Email, acc);
}
}
SOQL Lib
Map<String, Account> createdByEmailToAccount = (Map<String, Account>) SOQLEvaluator.of([
SELECT Id, CreatedBy.Email
FROM Account
WITH USER_MODE
]).toMap('CreatedBy', User.Email);
toMap with custom key and value
Apex
Map<String, String> accountNameToIndustry = new Map<String, String>();
for (Account acc : [SELECT Id, Name, Industry FROM Account WITH USER_MODE]) {
accountNameToIndustry.put(acc.Name, acc.Industry);
}
SOQL Lib
Map<String, String> accountNameToIndustry = SOQLEvaluator.of([
SELECT Id, Name, Industry
FROM Account
WITH USER_MODE
]).toMap(Account.Name, Account.Industry);
toAggregatedMap
Apex
Map<String, List<Account>> industryToAccounts = new Map<String, List<Account>>();
for (Account acc : [SELECT Id, Name, Industry FROM Account WITH USER_MODE]) {
if (!industryToAccounts.containsKey(acc.Industry)) {
industryToAccounts.put(acc.Industry, new List<Account>());
}
industryToAccounts.get(acc.Industry).add(acc);
}
SOQL Lib
Map<String, List<Account>> industryToAccounts = (Map<String, List<Account>>) SOQLEvaluator.of([
SELECT Id, Name, Industry
FROM Account
WITH USER_MODE
]).toAggregatedMap(Account.Industry);
toAggregatedMap with relationship key
Apex
Map<String, List<Account>> createdByEmailToAccounts = new Map<String, List<Account>>();
for (Account acc : [SELECT Id, Name, CreatedBy.Email FROM Account WITH USER_MODE]) {
String email = acc.CreatedBy?.Email;
if (email != null) {
if (!createdByEmailToAccounts.containsKey(email)) {
createdByEmailToAccounts.put(email, new List<Account>());
}
createdByEmailToAccounts.get(email).add(acc);
}
}
SOQL Lib
Map<String, List<Account>> createdByEmailToAccounts = (Map<String, List<Account>>) SOQLEvaluator.of([
SELECT Id, Name, CreatedBy.Email
FROM Account
WITH USER_MODE
]).toAggregatedMap('CreatedBy', User.Email);
toAggregatedMap with custom key and value
Apex
Map<String, List<String>> industryToAccountNames = new Map<String, List<String>>();
for (Account acc : [SELECT Id, Name, Industry FROM Account WITH USER_MODE]) {
if (!industryToAccountNames.containsKey(acc.Industry)) {
industryToAccountNames.put(acc.Industry, new List<String>());
}
industryToAccountNames.get(acc.Industry).add(acc.Name);
}
SOQL Lib
Map<String, List<String>> industryToAccountNames = SOQLEvaluator.of([
SELECT Id, Name, Industry
FROM Account
WITH USER_MODE
]).toAggregatedMap(Account.Industry, Account.Name);