Override getRecordsWithNotBlankFieldValues for non-String fields (#90)
* Override getRecordsWithNotBlankFieldValues in fflib_SObjects2 The base fflib_SObjects implementation casts every value to String, which throws a TypeException on Date/Datetime/Number/Lookup fields. This override generalises the check to "any non-null value, and for Strings also non-blank" so the helper works for every field type. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * Update fflib_SObjects2.cls Rename method to getRecordsWithNonBlankFieldValues * Update fflib_SObjects2Test.cls Rename method to getRecordsWithNonBlankFieldValues * Update fflib_SObjects2.cls Rename variable Co-authored-by: Robin Weymans <robin.weymans@mylighthouse.com> Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com> Co-authored-by: William Velzeboer <6429417+wimvelzeboer@users.noreply.github.com>
This commit is contained in:
co-authored by
Claude Opus 4.7
Robin Weymans
William Velzeboer
parent
368978bf20
commit
94129a9791
@@ -1078,6 +1078,29 @@ public virtual class fflib_SObjects2
|
||||
return getRecordsNotIn(sObjectField, new Set<String>{ value });
|
||||
}
|
||||
|
||||
/**
|
||||
* Overrides the fflib_SObjects base implementation, which casts every value to `String` and
|
||||
* uses `String.isNotBlank` — that throws a TypeException on Date / Datetime / Number / Lookup
|
||||
* fields. This override generalises to "any non-null value, and for Strings also non-blank",
|
||||
* so the helper works for every field type.
|
||||
*
|
||||
* @param field The field to check.
|
||||
*
|
||||
* @return The records whose value for `field` is non-null (and, for Strings, non-blank).
|
||||
*/
|
||||
protected override List<SObject> getRecordsWithNonBlankFieldValues(Schema.SObjectField sObjectField)
|
||||
{
|
||||
List<SObject> result = new List<SObject>();
|
||||
for (SObject record : getRecords())
|
||||
{
|
||||
Object value = record.get(sObjectField);
|
||||
if (value == null) continue;
|
||||
if (value instanceof String && String.isBlank((String) value)) continue;
|
||||
result.add(record);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@TestVisible
|
||||
protected virtual List<SObject> getRecordsNotIn(Schema.SObjectField sObjectField, Set<String> values)
|
||||
{
|
||||
@@ -1455,4 +1478,4 @@ public virtual class fflib_SObjects2
|
||||
}
|
||||
|
||||
public class DeveloperException extends Exception { }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -679,6 +679,30 @@ private class fflib_SObjects2Test
|
||||
System.Assert.areEqual(3, domain.selectWithShippingCountry().size());
|
||||
}
|
||||
|
||||
@IsTest
|
||||
static void itShouldReturnRecordsWithNonNullNumericField()
|
||||
{
|
||||
DomainAccounts domain = generateDomain();
|
||||
|
||||
System.Assert.areEqual(4, domain.selectWithAnnualRevenue().size());
|
||||
}
|
||||
|
||||
@IsTest
|
||||
static void itShouldReturnRecordsWithNonNullDatetimeField()
|
||||
{
|
||||
Datetime now = Datetime.now();
|
||||
DomainAccounts domain = new DomainAccounts(
|
||||
new List<Account>
|
||||
{
|
||||
(Account) fflib_MockSObjectUtil.addFieldValue(new Account(Name = 'A'), Account.CreatedDate, now),
|
||||
(Account) fflib_MockSObjectUtil.addFieldValue(new Account(Name = 'B'), Account.CreatedDate, now),
|
||||
new Account(Name = 'C')
|
||||
}
|
||||
);
|
||||
|
||||
System.Assert.areEqual(2, domain.selectWithCreatedDate().size());
|
||||
}
|
||||
|
||||
@IsTest
|
||||
static void itShouldReturnRecordsWithAllFieldValues()
|
||||
{
|
||||
@@ -845,11 +869,25 @@ private class fflib_SObjects2Test
|
||||
|
||||
public List<Account> selectWithShippingCountry()
|
||||
{
|
||||
return getRecordsWithNotBlankFieldValues(
|
||||
return getRecordsWithNonBlankFieldValues(
|
||||
Schema.Account.ShippingCountry
|
||||
);
|
||||
}
|
||||
|
||||
public List<Account> selectWithAnnualRevenue()
|
||||
{
|
||||
return getRecordsWithNonBlankFieldValues(
|
||||
Schema.Account.AnnualRevenue
|
||||
);
|
||||
}
|
||||
|
||||
public List<Account> selectWithCreatedDate()
|
||||
{
|
||||
return getRecordsWithNonBlankFieldValues(
|
||||
Schema.Account.CreatedDate
|
||||
);
|
||||
}
|
||||
|
||||
public List<Account> selectWithEmptyRecord()
|
||||
{
|
||||
return getRecordsWithAllBlankFieldValues(
|
||||
|
||||
Reference in New Issue
Block a user