Add getDecimalFieldSum method to domain
This will summarise the Decimal field value Signed-off-by: WimVelzeboer <wimvelzeboer@protonmail.com>
This commit is contained in:
@@ -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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user