Add getDecimalFieldSum method to domain

This will summarise the Decimal field value

Signed-off-by: WimVelzeboer <wimvelzeboer@protonmail.com>
This commit is contained in:
WimVelzeboer
2021-11-03 15:24:05 +00:00
parent 5b486bc3a2
commit 6eee1acdb6
2 changed files with 28 additions and 4 deletions
@@ -249,6 +249,22 @@ public virtual class fflib_SObjects2
.getDateTimeFieldValues(sObjectField);
}
/**
* @param sObjectField The Schema.SObjectField to get the SUM of
*
* @return The total SUM of the given field
*/
@TestVisible
protected virtual Decimal getDecimalFieldSum(Schema.SObjectField sObjectField)
{
Decimal result = 0;
for (SObject record : getRecords())
{
result += record.get(sObjectField) == null ? 0 : (Decimal) record.get(sObjectField);
}
return result;
}
/**
* @param sObjectField The Schema.SObjectField to get their Id values
*
@@ -319,15 +319,23 @@ private class fflib_SObjects2Test
System.assert(domain.selectByRating('Hot').size() == 1);
}
@IsTest
static void itShouldGetTheSumOfAnnualRevenue()
{
DomainAccounts domain = generateDomain();
System.assert(domain.getDecimalFieldSum(Account.AnnualRevenue) == 11);
System.assert(domain.getDecimalFieldValues(Account.AnnualRevenue).contains((Decimal) 5));
}
private static DomainAccounts generateDomain()
{
DomainAccounts domain = new DomainAccounts(
new List<Account>
{
new Account(Name = 'A', ShippingCountry = 'USA'),
new Account(Name = 'B', ShippingCountry = 'Ireland'),
new Account(Name = 'C', ShippingCountry = 'UK'),
new Account(Name = 'D', ShippingCountry = ''),
new Account(Name = 'A', ShippingCountry = 'USA', AnnualRevenue = 5),
new Account(Name = 'B', ShippingCountry = 'Ireland', AnnualRevenue = 3),
new Account(Name = 'C', ShippingCountry = 'UK', AnnualRevenue = 2),
new Account(Name = 'D', ShippingCountry = '', AnnualRevenue = 1),
new Account(Name = 'E'),
new Account()
}