From bc62708b8839a0472c459bd5f25ca69dba608b15 Mon Sep 17 00:00:00 2001 From: Jan Aertgeerts Date: Mon, 10 Jan 2022 11:49:20 +0100 Subject: [PATCH 1/2] add getIdFieldByStringField() --- docs/domains/sobject.md | 13 +++++++++ .../classes/domains/fflib_SObjects2.cls | 29 +++++++++++++++++++ .../classes/domains/fflib_SObjects2Test.cls | 13 +++++++++ 3 files changed, 55 insertions(+) diff --git a/docs/domains/sobject.md b/docs/domains/sobject.md index dcc2488..601355d 100644 --- a/docs/domains/sobject.md +++ b/docs/domains/sobject.md @@ -29,6 +29,7 @@ Protected methods - [getDoubleFieldValues](#getdoublefieldvalues) - [getFieldByField](#getfieldbyfield) - [getFieldsByField](#getfieldsbyfield) +- [getIdFieldByStringField](#getidfieldbystringfield) - [getIdFieldByIdField](#getidfieldbyidfield) - [getIdFieldsByIdField](#getidfieldsbyidfield) - [getIdFieldValues](#getidfieldvalues) @@ -138,6 +139,18 @@ Contacts contacts = Contacts.newInstance(records); Map> contactIdByAccountId = contacts.getFieldsByField(Contact.Id, Contact.AccountId); ```

+#### getIdFieldByStringField +Get a map with the string field as the key and the ID as values. Key fields containing null values are omitted. + +`protected Map getIdFieldByStringField(Schema.SObjectField valueField, Schema.SObjectField keyField)` + +_Example_ + +```apex +Contacts contacts = Contacts.newInstance(records); +Map accountIdByContactId = contacts.getIdFieldByStringField(Contact.Id, Contact.Name); +``` +

#### getIdFieldByIdField Get a map with the values of two Id fields. Key fields containing null values are omitted. 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 cb93eb7..0326647 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 @@ -397,6 +397,35 @@ public virtual class fflib_SObjects2 return getRecords().get(0); } + /** + * 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 + * Contacts contacts = Contacts.newInstance(records); + * Map accountIdByContactId = contacts.getIdFieldByStringField(Contact.Id, Contact.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; + } + /** * Get a map with the values of two Id fields * Key fields containing null values are omitted 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 96057d8..9bdda31 100644 --- a/sfdx-source/apex-extensions/tests/classes/domains/fflib_SObjects2Test.cls +++ b/sfdx-source/apex-extensions/tests/classes/domains/fflib_SObjects2Test.cls @@ -124,6 +124,19 @@ private class fflib_SObjects2Test // todo - add assertions } + @IsTest + static void itShouldReturnStringById() + { + final Id accountId = fflib_IDGenerator.generate(Schema.Account.SObjectType); + final Account record = new Account(Id = accountId, Name = 'Test'); + fflib_SObjects2 domain = new fflib_SObjects2(new List{ record }); + System.Test.startTest(); + Map nameById = domain.getIdFieldByStringField(Schema.Account.Id, Schema.Account.Name); + System.Test.stopTest(); + System.assert(nameById.containsKey('Test')); + System.assertEquals(accountId, nameById.get(accountId)); + } + @IsTest static void itShouldReturnRecordIdById() { From 89c2e2362527cb388919e35d9016adb091ae9d3e Mon Sep 17 00:00:00 2001 From: William Velzeboer <6429417+wimvelzeboer@users.noreply.github.com> Date: Wed, 12 Jan 2022 11:14:02 +0000 Subject: [PATCH 2/2] Fix failing unit-test --- .../tests/classes/domains/fflib_SObjects2Test.cls | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) 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 a8dec05..00d97b2 100644 --- a/sfdx-source/apex-extensions/tests/classes/domains/fflib_SObjects2Test.cls +++ b/sfdx-source/apex-extensions/tests/classes/domains/fflib_SObjects2Test.cls @@ -124,14 +124,15 @@ private class fflib_SObjects2Test @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 = 'Test'); + final Account record = new Account(Id = accountId, Name = accountName); fflib_SObjects2 domain = new fflib_SObjects2(new List{ record }); System.Test.startTest(); Map nameById = domain.getIdFieldByStringField(Schema.Account.Id, Schema.Account.Name); System.Test.stopTest(); - System.assert(nameById.containsKey('Test')); - System.assertEquals(accountId, nameById.get(accountId)); + System.assert(nameById.containsKey(accountName)); + System.assertEquals(accountId, nameById.get(accountName)); } @IsTest @@ -591,4 +592,4 @@ private class fflib_SObjects2Test clearField(Schema.Account.ShippingCountry); } } -} \ No newline at end of file +}