SOQL Evaluator
Apex Classes: SOQLEvaluator.cls and SOQLEvaluator_Test.cls.
The lib evaluator class for processing of queried records.
List<Account> accounts = SOQLEvaluator.of([
SELECT Id, Name
FROM Account
WITH USER_MODE
]).toList();
Methodsโ
The following are methods for using SOQLEvaluator:
mockId(String mockId)SOQLEvaluator.mock(String mockId).thenReturn(SObject record)SOQLEvaluator.mock(String mockId).thenReturn(List<SObject> records)
toId()toIds()toIdsOf(SObjectField field)toIdsOf(String relationshipName, SObjectField field)doExist()toValueOf(SObjectField fieldToExtract)toValuesOf(SObjectField fieldToExtract)toObject()toList()toMap()toMap(SObjectField keyField)toMap(String relationshipName, SObjectField targetKeyField)toMap(SObjectField keyField, SObjectField valueField)toAggregatedMap(SObjectField keyField)toAggregatedMap(String relationshipName, SObjectField targetKeyField)toAggregatedMap(SObjectField keyField, SObjectField valueField)
INITโ
ofโ
Constructs a SOQLEvaluator from existing SObject records.
Signature
SObjectEvaluable of(List<SObject> staticQueryRecords)
Example
SOQLEvaluator.of([
SELECT Id, Name, Industry
FROM Account
WITH USER_MODE
]).toList();
FIELD-LEVEL SECURITYโ
stripInaccessibleโ
The Security.stripInaccessible method removes inaccessible fields from the existing records based on the current user's permissions.
Signature
SObjectEvaluable stripInaccessible()
SObjectEvaluable stripInaccessible(AccessType accessType)
Example
List<Account> accessibleAccounts = SOQLEvaluator.of([
SELECT Id, Name, Industry
FROM Account
WITH USER_MODE
]).stripInaccessible().toList();
// or with specific access type
List<Account> readableAccounts = SOQLEvaluator.of([
SELECT Id, Name, Industry
FROM Account
WITH USER_MODE
]).stripInaccessible(AccessType.READABLE).toList();
MOCKINGโ
mockIdโ
Evaluator needs unique id that allows for mocking.
Signature
SObjectEvaluable mockId(String mockId)
Example
SOQLEvaluator.of([
SELECT Id, Name
FROM Account
WITH USER_MODE
]).mockId('MyEvaluator').toList();
// In Unit Test
SOQLEvaluator.mock('MyEvaluator').thenReturn(new List<Account>{
new Account(Name = 'Mocked Account 1'),
new Account(Name = 'Mocked Account 2')
});
record mockโ
Signature
SOQLEvaluator.Mockable mock(String mockId).thenReturn(SObject record)
Example
SOQLEvaluator.of([
SELECT Id, Name
FROM Account
WITH USER_MODE
]).mockId('MyEvaluator').toObject();
// In Unit Test
SOQLEvaluator.mock('MyEvaluator').thenReturn(new Account(Name = 'Mocked Account'));
records mockโ
Signature
SOQLEvaluator.Mockable mock(String mockId).thenReturn(List<SObject> records)
Example
SOQLEvaluator.of([
SELECT Id, Name
FROM Account
WITH USER_MODE
]).mockId('MyEvaluator').toList();
// In Unit Test
SOQLEvaluator.mock('MyEvaluator').thenReturn(new List<Account>{
new Account(Name = 'Mocked Account 1'),
new Account(Name = 'Mocked Account 2')
});
RESULTโ
toIdโ
Signature
Id toId()
Example
Id accountId = SOQLEvaluator.of([
SELECT Id, Name
FROM Account
WITH USER_MODE
LIMIT 1
]).toId();
toIdsโ
Extract all record IDs from the collection.
Signature
Set<Id> toIds()
Example
Set<Id> accountIds = SOQLEvaluator.of([
SELECT Id, Name
FROM Account
WITH USER_MODE
]).toIds();
toIdsOfโ
Extract field values as Set of IDs from the collection.
Signature
Set<Id> toIdsOf(SObjectField field)
Example
Set<Id> ownerIds = SOQLEvaluator.of([
SELECT Id, Name, OwnerId
FROM Account
WITH USER_MODE
]).toIdsOf(Account.OwnerId);
toIdsOf Related Fieldโ
Signature
Set<Id> toIdsOf(String relationshipName, SObjectField field)
Example
Set<Id> parentAccountIds = SOQLEvaluator.of([
SELECT Id, Name, Parent.Id
FROM Account
WITH USER_MODE
]).toIdsOf('Parent', Account.Id);
doExistโ
Signature
Boolean doExist()
Example
Boolean recordsExist = SOQLEvaluator.of([
SELECT Id
FROM Account
WHERE Name = 'Test'
WITH USER_MODE
]).doExist();
toValueOfโ
Extract field value from the first record in the collection.
Signature
Object toValueOf(SObjectField fieldToExtract)
Example
String accountName = (String) SOQLEvaluator.of([
SELECT Id, Name, Industry
FROM Account
WITH USER_MODE
LIMIT 1
]).toValueOf(Account.Name);
toValuesOfโ
Extract field values from all records in the collection.
Signature
Set<String> toValuesOf(SObjectField fieldToExtract)
Example
Set<String> accountNames = SOQLEvaluator.of([
SELECT Id, Name
FROM Account
WITH USER_MODE
]).toValuesOf(Account.Name);
toObjectโ
When the collection contains more than one record, the error List has more than 1 row for assignment to SObject will occur.
When there are no records to assign, the error List has no rows for assignment to SObject will NOT occur. This is automatically handled by the framework, and a null value will be returned instead.
Signature
SObject toObject()
Example
Account account = (Account) SOQLEvaluator.of([
SELECT Id, Name
FROM Account
WITH USER_MODE
LIMIT 1
]).toObject();
toListโ
Signature
List<SObject> toList()
Example
List<Account> processedAccounts = SOQLEvaluator.of([
SELECT Id, Name
FROM Account
WITH USER_MODE
]).toList();
toMapโ
Signature
Map<Id, SObject> toMap()
Example
Map<Id, Account> idToAccount = (Map<Id, Account>) SOQLEvaluator.of([
SELECT Id, Name
FROM Account
WITH USER_MODE
]).toMap();
toMap with custom keyโ
Signature
Map<String, SObject> toMap(SObjectField keyField)
Example
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โ
Signature
Map<String, SObject> toMap(String relationshipName, SObjectField targetKeyField)
Example
Map<String, Account> createdByEmailToAccount = (Map<String, Account>) SOQLEvaluator.of([
SELECT Id, Name, CreatedBy.Email
FROM Account
WITH USER_MODE
]).toMap('CreatedBy', User.Email);
toMap with custom key and valueโ
Signature
Map<String, String> toMap(SObjectField keyField, SObjectField valueField)
Example
Map<String, String> nameToIndustry = SOQLEvaluator.of([
SELECT Id, Name, Industry
FROM Account
WITH USER_MODE
]).toMap(Account.Name, Account.Industry);
toAggregatedMapโ
Signature
Map<String, List<SObject>> toAggregatedMap(SObjectField keyField)
Example
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 custom relationship keyโ
Signature
Map<String, List<SObject>> toAggregatedMap(String relationshipName, SObjectField targetKeyField)
Example
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 valueโ
Signature
Map<String, List<String>> toAggregatedMap(SObjectField keyField, SObjectField valueField)
Example
Map<String, List<String>> industryToAccountNames = SOQLEvaluator.of([
SELECT Id, Name, Industry
FROM Account
WITH USER_MODE
]).toAggregatedMap(Account.Industry, Account.Name);