From 6387c51bde84f2257e04d79f3e3e2e79606543fc Mon Sep 17 00:00:00 2001 From: Piotr PG Gajek Date: Mon, 9 Oct 2023 17:26:33 +0200 Subject: [PATCH] toMap methods --- force-app/main/default/classes/SOQL.cls | 39 ++++++++++++++++++++ force-app/main/default/classes/SOQL_Test.cls | 34 ++++++++++++++++- website/docs/api/soql.md | 33 ++++++++++++++++- 3 files changed, 104 insertions(+), 2 deletions(-) diff --git a/force-app/main/default/classes/SOQL.cls b/force-app/main/default/classes/SOQL.cls index 64bb6d3..4e86e88 100644 --- a/force-app/main/default/classes/SOQL.cls +++ b/force-app/main/default/classes/SOQL.cls @@ -127,7 +127,9 @@ public virtual inherited sharing class SOQL implements Queryable { List toAggregated(); Map toMap(); Map toMap(SObjectField keyField); + Map toMap(SObjectField keyField, SObjectField valueField); Map> toAggregatedMap(SObjectField keyField); + Map> toAggregatedMap(SObjectField keyField, SObjectField valueField); Database.QueryLocator toQueryLocator(); } @@ -588,6 +590,23 @@ public virtual inherited sharing class SOQL implements Queryable { return cutomKeyToRecord; } + public Map toMap(SObjectField keyField, SObjectField valueField) { + builder.fields.clearAllFields(); // other fields not needed + + with(keyField, valueField); + + Map cutomKeyToCustomValue = new Map(); + + for (SObject record : toList()) { + cutomKeyToCustomValue.put( + String.valueOf(record.get(keyField)), + String.valueOf(record.get(valueField)) + ); + } + + return cutomKeyToCustomValue; + } + public Map> toAggregatedMap(SObjectField keyField) { with(keyField); @@ -606,6 +625,26 @@ public virtual inherited sharing class SOQL implements Queryable { return cutomKeyToRecords; } + public Map> toAggregatedMap(SObjectField keyField, SObjectField valueField) { + builder.fields.clearAllFields(); // other fields not needed + + with(keyField, valueField); + + Map> customKeyToValues = new Map>(); + + for (SObject record : toList()) { + String customKey = String.valueOf(record.get(keyField)); + + if (!customKeyToValues.containsKey(customKey)) { + customKeyToValues.put(customKey, new List()); + } + + customKeyToValues.get(customKey).add(String.valueOf(record.get(valueField))); + } + + return customKeyToValues; + } + public Database.QueryLocator toQueryLocator() { return executor.toQueryLocator(builder.toString(), binder.getBindingMap()); } diff --git a/force-app/main/default/classes/SOQL_Test.cls b/force-app/main/default/classes/SOQL_Test.cls index 1f4e810..677f90f 100644 --- a/force-app/main/default/classes/SOQL_Test.cls +++ b/force-app/main/default/classes/SOQL_Test.cls @@ -1921,6 +1921,22 @@ private class SOQL_Test { } } + @IsTest + static void toMapWithCustomKeyAndCustomValue() { + // Setup + List accounts = insertAccounts(); + + // Test + Map result = SOQL.of(Account.SObjectType).toMap(Account.Name, Account.Id); + + // Verify + Assert.areEqual(accounts.size(), result.size()); + + for (Account acc : accounts) { + Assert.isNotNull(result.get(acc.Name)); + } + } + @IsTest static void toAggregatedMapWithCustomKey() { // Setup @@ -1933,7 +1949,7 @@ private class SOQL_Test { Assert.areEqual(accounts.size(), result.size()); for (Account acc : accounts) { - Assert.isNotNull(result.get(acc.Name)); + Assert.isFalse(result.get(acc.Name).isEmpty()); } } @@ -1949,6 +1965,22 @@ private class SOQL_Test { Assert.areEqual(1, result.size()); // grouped by empty Industry } + @IsTest + static void toAggregatedMapWithCustomKeyAndCustomValue() { + // Setup + List accounts = insertAccounts(); + + // Test + Map> result = SOQL.of(Account.SObjectType).toAggregatedMap(Account.Name, Account.Id); + + // Verify + Assert.areEqual(accounts.size(), result.size()); + + for (Account acc : accounts) { + Assert.isFalse(result.get(acc.Name).isEmpty()); + } + } + @IsTest static void toQueryLocator() { // Test diff --git a/website/docs/api/soql.md b/website/docs/api/soql.md index e1541c3..f3fd609 100644 --- a/website/docs/api/soql.md +++ b/website/docs/api/soql.md @@ -129,7 +129,9 @@ The following are methods for `SOQL`. - [`toAggregated()`](#toaggregated) - [`toMap()`](#tomap) - [`toMap(SObjectField keyField)`](#tomap-with-custom-key) +- [`toMap(SObjectField keyField, SObjectField valueField)`](#tomap-with-custom-key-and-value) - [`toAggregatedMap(SObjectField keyField)`](#toaggregatedmap) +- [`toAggregatedMap(SObjectField keyField, SObjectField valueField)`](#toaggregatedmap-with-custom-value) - [`toQueryLocator()`](#toquerylocator) ## INIT @@ -1479,7 +1481,7 @@ Map toMap() **Example** ```apex -SOQL.of(Account.SObjectType).toMap(); +Map idToAccount = (Map) SOQL.of(Account.SObjectType).toMap(); ``` ### toMap with custom key @@ -1496,6 +1498,21 @@ Map toMap(SObjectField keyField) Map nameToAccount = (Map) SOQL.of(Account.SObjectType).toMap(Account.Name); ``` +### toMap with custom key and value + +**Signature** + +```apex +Map toMap(SObjectField keyField, , SObjectField valueField) +``` + +**Example** + +```apex +Map nameToAccount = SOQL.of(Account.SObjectType).toMap(Account.Name, Account.Industry); +``` + + ### toAggregatedMap **Signature** @@ -1510,6 +1527,20 @@ Map> toAggregatedMap(SObjectField keyField) Map industryToAccounts = SOQL.of(Account.SObjectType).toAggregatedMap(Account.Industry); ``` +### toAggregatedMap with custom value + +**Signature** + +```apex +Map> toAggregatedMap(SObjectField keyField, SObjectField valueField) +``` + +**Example** + +```apex +Map> industryToAccounts = SOQL.of(Account.SObjectType).toAggregatedMap(Account.Industry, Account.Name); +``` + ### toQueryLocator **Signature**