From 6586ed6bb24700a1fbd26344e3dfb27b8a71ac5c Mon Sep 17 00:00:00 2001 From: Wim Velzeboer Date: Fri, 5 Apr 2024 11:36:08 +0100 Subject: [PATCH] Add domain method getIdFieldsByStringField --- .../classes/domains/fflib_SObjects2.cls | 31 +++++++++++++++++++ .../classes/domains/fflib_SObjects2Test.cls | 2 ++ 2 files changed, 33 insertions(+) diff --git a/sfdx-source/apex-extensions/main/default/classes/domains/fflib_SObjects2.cls b/sfdx-source/apex-extensions/main/default/classes/domains/fflib_SObjects2.cls index 8c554f6..9181259 100644 --- a/sfdx-source/apex-extensions/main/default/classes/domains/fflib_SObjects2.cls +++ b/sfdx-source/apex-extensions/main/default/classes/domains/fflib_SObjects2.cls @@ -626,6 +626,37 @@ public virtual class fflib_SObjects2 return result; } + /** + * Get a map with the values of an Id field by a String field with a one to many relation + * Key fields containing null values are omitted + * + * @param valueField The Id field to use as the Value of the Map + * @param keyField The String field to use as the Key of the map + * + * @return Returns a map with the values of two fields + * + * @example + * Accounts accounts = Accounts.newInstance(records); + * Map> accountIdsByCountry = accounts.getIdFieldsByStringField(Account.BillingCountry, Account.Id); + */ + @TestVisible + protected virtual Map> getIdFieldsByStringField(Schema.SObjectField valueField, Schema.SObjectField keyField) + { + Map> result = new Map>(); + for (SObject record : getRecords()) + { + String keyFieldString = String.valueOf(record.get(keyField)); + if (keyFieldString == null) continue; + + if (!result.containsKey(keyFieldString)) + { + result.put(keyFieldString, new Set()); + } + result.get(keyFieldString).add((Id) record.get(valueField)); + } + return result; + } + /** * @param sObjectField The Schema.SObjectField to get their Id values * @param criteria The criteria condition that must be met to include the value in the return diff --git a/sfdx-source/apex-extensions/tests/classes/domains/fflib_SObjects2Test.cls b/sfdx-source/apex-extensions/tests/classes/domains/fflib_SObjects2Test.cls index 527c32b..506b241 100644 --- a/sfdx-source/apex-extensions/tests/classes/domains/fflib_SObjects2Test.cls +++ b/sfdx-source/apex-extensions/tests/classes/domains/fflib_SObjects2Test.cls @@ -105,6 +105,7 @@ private class fflib_SObjects2Test domain.getFieldsByField(Account.Id, Account.AnnualRevenue); domain.getFieldByField(Account.Id, Account.Id); domain.getIdFieldsByIdField(Account.Id, Account.Id); + domain.getIdFieldsByStringField(Account.Name, Account.Id); domain.getIdFieldValues(Account.Id); domain.getIdFieldValues(Account.Id, criteria); domain.getDecimalFieldValues(Account.BillingLatitude, criteria); @@ -116,6 +117,7 @@ private class fflib_SObjects2Test domain.getSObjectsByStringField(Account.Name); domain.getStringFieldByStringField(Account.Name, Account.AccountNumber); domain.getStringFieldValues(Account.Name, criteria); + domain.stri // todo - add assertions }