From 0b2d78ee5ae1411941781f4732f00b24fe51ee83 Mon Sep 17 00:00:00 2001 From: wimvelzeboer Date: Wed, 6 Apr 2022 11:22:18 +0100 Subject: [PATCH 1/2] Add methods to retrieve maps form domains --- .../main/classes/fflib_SObjects.cls | 77 +++++++++++++++++++ .../test/classes/fflib_SObjectsTest.cls | 49 ++++++++++++ 2 files changed, 126 insertions(+) diff --git a/sfdx-source/apex-common/main/classes/fflib_SObjects.cls b/sfdx-source/apex-common/main/classes/fflib_SObjects.cls index 92089ace871..69e709ba80a 100644 --- a/sfdx-source/apex-common/main/classes/fflib_SObjects.cls +++ b/sfdx-source/apex-common/main/classes/fflib_SObjects.cls @@ -111,6 +111,58 @@ public virtual class fflib_SObjects return result; } + /** + * Get a map with the values of two Id fields + * Key fields containing null values are omitted + * + * @param valueField The Id field to use as the Value of the Map + * @param keyField The Id field to use as the Key of the map + * + * @return Returns a map with the values of two fields + * + * @example + * Contacts contacts = Contacts.newInstance(records); + * Map accountIdByContactId = contacts.getIdFieldByIdField(Contact.AccountId, Contact.Id); + */ + @TestVisible + protected virtual Map getIdFieldByIdField(Schema.SObjectField valueField, Schema.SObjectField keyField) + { + Map result = new Map(); + for (SObject record : getRecords()) + { + if (record.get(keyField) == null) continue; + + result.put((Id) record.get(keyField), (Id) record.get(valueField)); + } + return result; + } + + /** + * Get a map with the values of a String field as key and Id field + * 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 accountIdByName = accounts.getIdFieldByStringField(Schema.Account.Id, Schema.Account.Name); + */ + @TestVisible + protected virtual Map getIdFieldByStringField(Schema.SObjectField valueField, Schema.SObjectField keyField) + { + Map result = new Map(); + for (SObject record : getRecords()) + { + if (record.get(keyField) == null) continue; + + result.put((String) record.get(keyField), (Id) record.get(valueField)); + } + return result; + } + /** * @param sObjectField The Schema.SObjectField to compare against the given value * @param value The given value of the records sObjectField to include in the return @@ -257,6 +309,31 @@ public virtual class fflib_SObjects return result; } + /** + * Get a map with the given String field value mapped to the given Id field + * Key fields containing null values are omitted + * + * @param valueField The String field to use as value for the map + * @param keyField The Id field to use as key for the map + * + * @return a map with the given String field value mapped to the given Id field + * + * @example + * Account account = Account.newInstance(records); + * Map accountNameById = account.getStringFieldByIdField(Account.AccountName, Account.Id); + */ + @TestVisible + protected virtual Map getStringFieldByIdField(Schema.SObjectField valueField, Schema.SObjectField keyField) + { + Map result = new Map(); + for (SObject record : getRecords()) + { + if (record.get(keyField) == null) continue; + + result.put((Id) record.get(keyField), (String) record.get(valueField)); + } + return result; + } protected virtual void setFieldValue(Schema.SObjectField sObjectField, Object value) { diff --git a/sfdx-source/apex-common/test/classes/fflib_SObjectsTest.cls b/sfdx-source/apex-common/test/classes/fflib_SObjectsTest.cls index d9dcd756b1d..521c331847a 100644 --- a/sfdx-source/apex-common/test/classes/fflib_SObjectsTest.cls +++ b/sfdx-source/apex-common/test/classes/fflib_SObjectsTest.cls @@ -85,6 +85,55 @@ private class fflib_SObjectsTest System.assert(domain.selectPopulatedRecords().size() == 4); } + @IsTest + static void itShouldReturnRecordIdById() + { + final Id contactId = fflib_IDGenerator.generate(Schema.Contact.SObjectType); + final Id accountId = fflib_IDGenerator.generate(Schema.Account.SObjectType); + final Contact record = new Contact(Id = contactId, AccountId = accountId); + fflib_SObjects domain = new fflib_SObjects(new List{ record }); + System.Test.startTest(); + Map accountIdByContactId = domain.getIdFieldByIdField(Schema.Contact.AccountId, Schema.Contact.Id); + System.Test.stopTest(); + System.assert(accountIdByContactId.containsKey(contactId)); + System.assertEquals(accountId, accountIdByContactId.get(contactId)); + } + + @IsTest + static void itShouldReturnStringById() + { + final String accountName = 'My Account'; + final Id accountId = fflib_IDGenerator.generate(Schema.Account.SObjectType); + final Account record = new Account(Id = accountId, Name = accountName); + fflib_SObjects domain = new fflib_SObjects(new List{ record }); + System.Test.startTest(); + Map nameById = domain.getIdFieldByStringField(Schema.Account.Id, Schema.Account.Name); + System.Test.stopTest(); + System.assert(nameById.containsKey(accountName)); + System.assertEquals(accountId, nameById.get(accountName)); + } + + @IsTest + static void itShouldReturnStringFieldByIdField() + { + final String accountName = 'My Account'; + final Id accountId = fflib_IDGenerator.generate(Account.SObjectType); + fflib_SObjects domain = new fflib_SObjects( + new List + { + new Account(Id = accountId, Name = accountName), + new Account(Name = accountName + ' 2') // This one should be ignored + }); + + System.Test.startTest(); + Map result = domain.getStringFieldByIdField(Account.Name, Account.Id); + System.Test.stopTest(); + + System.assertEquals(1, result.size(), 'Incorrect returned amount of results'); + System.assert(result.keySet().contains(accountId), 'The accountId is missing from the results'); + System.assertEquals(accountName, result.get(accountId), 'Incorrect returned account name'); + } + @IsTest static void itShouldSetFieldValue() { From 34acdc20c34fe3825f264090e5e219242e147314 Mon Sep 17 00:00:00 2001 From: William Velzeboer <6429417+wimvelzeboer@users.noreply.github.com> Date: Fri, 27 Jan 2023 22:00:08 +0000 Subject: [PATCH 2/2] Update fflib_SObjects.cls Fix merge issue --- sfdx-source/apex-common/main/classes/fflib_SObjects.cls | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sfdx-source/apex-common/main/classes/fflib_SObjects.cls b/sfdx-source/apex-common/main/classes/fflib_SObjects.cls index 756b5d1a9cf..e792950afde 100644 --- a/sfdx-source/apex-common/main/classes/fflib_SObjects.cls +++ b/sfdx-source/apex-common/main/classes/fflib_SObjects.cls @@ -426,6 +426,7 @@ public virtual class fflib_SObjects return result; } + /** * Modifies a value of a field for all records in the domain * * @param field The reference to the SObjectField to be modified @@ -538,4 +539,4 @@ public virtual class fflib_SObjects public String message; public fflib_ISObjects domain; } -} \ No newline at end of file +}