diff --git a/sfdx-source/apex-common/main/classes/fflib_SObjects.cls b/sfdx-source/apex-common/main/classes/fflib_SObjects.cls index 95c48ccf017..d6c61029306 100644 --- a/sfdx-source/apex-common/main/classes/fflib_SObjects.cls +++ b/sfdx-source/apex-common/main/classes/fflib_SObjects.cls @@ -161,12 +161,15 @@ public virtual class fflib_SObjects * @return Return a set with all the Id values of the given field */ @TestVisible - protected Set getIdFieldValues(Schema.SObjectField field) + protected virtual Set getIdFieldValues(Schema.SObjectField field) { Set result = new Set(); for (SObject record : getRecords()) { - result.add((Id) record.get(field)); + if (record.isSet(field)) + { + result.add((Id) record.get(field)); + } } return result; } @@ -177,11 +180,16 @@ public virtual class fflib_SObjects * @return Return a set with all the String values of the given field */ @TestVisible - protected Set getStringFieldValues(Schema.SObjectField field) + protected virtual Set getStringFieldValues(Schema.SObjectField field) { Set result = new Set(); for (SObject record : getRecords()) { + if (String.isBlank((String) record.get(field))) + { + continue; + } + result.add((String) record.get(field)); } return result; diff --git a/sfdx-source/apex-common/test/classes/fflib_SObjectsTest.cls b/sfdx-source/apex-common/test/classes/fflib_SObjectsTest.cls index dd5bd13f0a0..b205e626aef 100644 --- a/sfdx-source/apex-common/test/classes/fflib_SObjectsTest.cls +++ b/sfdx-source/apex-common/test/classes/fflib_SObjectsTest.cls @@ -132,22 +132,29 @@ private class fflib_SObjectsTest { DomainAccounts domain = generateDomain(); - final Set expected = new Set - { - null, - '', - 'Canada', - 'Ireland', - 'UK', - 'USA' - }; - System.assert( - domain.getStringFieldValues(Schema.Account.ShippingCountry).equals(expected) - ); - + // Return only string, omitting nulls and blanks + System.assertEquals( + new Set + { + 'Canada', + 'Ireland', + 'UK', + 'USA' + }, + domain.getStringFieldValues(Schema.Account.ShippingCountry)); + + // Return all the values, including nulls and blanks System.assert( domain.getFieldValues(Schema.Account.ShippingCountry) - .equals(expected) + .equals(new Set + { + 'Canada', + 'Ireland', + 'UK', + 'USA', + '', + null + }) ); }