diff --git a/sfdx-source/apex-common/main/classes/fflib_SObjects.cls b/sfdx-source/apex-common/main/classes/fflib_SObjects.cls index 95c48ccf017..e792950afde 100644 --- a/sfdx-source/apex-common/main/classes/fflib_SObjects.cls +++ b/sfdx-source/apex-common/main/classes/fflib_SObjects.cls @@ -203,6 +203,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 field The Schema.SObjectField to compare against the given value * @param value The given value of the records field to include in the return @@ -348,6 +400,32 @@ 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; + } + /** * Modifies a value of a field for all records in the domain * @@ -461,4 +539,4 @@ public virtual class fflib_SObjects public String message; public fflib_ISObjects domain; } -} \ No newline at end of file +} diff --git a/sfdx-source/apex-common/test/classes/fflib_SObjectsTest.cls b/sfdx-source/apex-common/test/classes/fflib_SObjectsTest.cls index dd5bd13f0a0..a8de081cc17 100644 --- a/sfdx-source/apex-common/test/classes/fflib_SObjectsTest.cls +++ b/sfdx-source/apex-common/test/classes/fflib_SObjectsTest.cls @@ -127,6 +127,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 itShouldReturnFieldValues() {