From 1f45c8d4c221cbe35278363f5242ea8f7f6ac527 Mon Sep 17 00:00:00 2001 From: WimVelzeboer Date: Thu, 4 Nov 2021 17:18:46 +0000 Subject: [PATCH] Add setter method to fflib_SObjects2 plus unit-test Signed-off-by: WimVelzeboer --- .../classes/domains/fflib_SObjects2.cls | 57 ++++++++++++++++ .../classes/domains/fflib_SObjects2Test.cls | 67 +++++++++++++++++-- 2 files changed, 120 insertions(+), 4 deletions(-) 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 f2c993c..cb93eb7 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 @@ -387,6 +387,16 @@ public virtual class fflib_SObjects2 return result; } + /** + * @return Returns only the first SObject contained in the domain. + */ + public SObject getFirstSObject() + { + if (isEmpty()) return null; + + return getRecords().get(0); + } + /** * Get a map with the values of two Id fields * Key fields containing null values are omitted @@ -751,4 +761,51 @@ public virtual class fflib_SObjects2 new fflib_SObjects2(getRecords(criteria), getSObjectType()) .setFieldValue(sObjectField, value); } + + /** + * Sets a value to the given field only when key field Id value is provided in the given map + * + * @param sObjectIdFieldToCheck The SObject Id Field to match the key against in the provided map + * @param sObjectFieldToUpdate The SObjectField to store the mapped value when the key matches the value in the sObjectFieldToUpdate field + * @param values Map of values to store by the sObjectIdFieldToCheck fields value + */ + @TestVisible + protected virtual void setFieldValue( + Schema.SObjectField sObjectIdFieldToCheck, + Schema.SObjectField sObjectFieldToUpdate, + Map values) + { + for (SObject record : getRecords()) + { + Id keyValue = (Id) record.get(sObjectIdFieldToCheck); + if (values?.containsKey(keyValue)) + { + record.put(sObjectFieldToUpdate, values.get(keyValue)); + } + } + } + + /** + * Sets a value to the given field only when key field String value is provided in the given map + * + * @param sObjectStringFieldToCheck The SObject String Field to match the key against in the provided map + * @param sObjectFieldToUpdate The SObjectField to store the mapped value when the key matches the value in the sObjectFieldToUpdate field + * @param values Map of values to store by the sObjectIdFieldToCheck fields value + */ + @TestVisible + protected virtual void setFieldValue( + Schema.SObjectField sObjectStringFieldToCheck, + Schema.SObjectField sObjectFieldToUpdate, + Map values) + { + for (SObject record : getRecords()) + { + String keyValue = (String) record.get(sObjectStringFieldToCheck); + if (values?.containsKey(keyValue)) + { + record.put(sObjectFieldToUpdate, values.get(keyValue)); + } + } + + } } \ No newline at end of file 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 8ae2b1c..d5c2f6c 100644 --- a/sfdx-source/apex-extensions/tests/classes/domains/fflib_SObjects2Test.cls +++ b/sfdx-source/apex-extensions/tests/classes/domains/fflib_SObjects2Test.cls @@ -89,11 +89,21 @@ private class fflib_SObjects2Test { fflib_Criteria criteria = new fflib_Criteria().equalTo(Account.Name, 'A'); - DomainAccounts domain = generateDomain(); + DomainAccounts domain = new DomainAccounts( + new List + { + new Account(Id = fflib_IDGenerator.generate(Account.SObjectType), Name = 'A', ShippingCountry = 'USA', AnnualRevenue = 5, BillingLatitude = 121.0), + new Account(Id = fflib_IDGenerator.generate(Account.SObjectType), Name = 'B', ShippingCountry = 'Ireland', AnnualRevenue = 3), + new Account(Id = fflib_IDGenerator.generate(Account.SObjectType), Name = 'C', ShippingCountry = 'UK', AnnualRevenue = 2), + new Account(Id = fflib_IDGenerator.generate(Account.SObjectType), Name = 'D', ShippingCountry = '', AnnualRevenue = 1), + new Account(Id = fflib_IDGenerator.generate(Account.SObjectType), Name = 'E'), + new Account(Id = fflib_IDGenerator.generate(Account.SObjectType), BillingLatitude = 121.0 ) + } + ); domain.getDateTimeFieldValues(Account.LastActivityDate); - domain.getDateTimeFieldValues(Account.CreatedDate); - domain.getFieldByField(Account.Name, Account.AccountNumber); - domain.getFieldsByField(Account.Id, Account.AccountNumber); + domain.getDateTimeFieldValues(Account.CreatedDate, criteria); + domain.getFieldByField(Account.Name, Account.AnnualRevenue); + domain.getFieldsByField(Account.Id, Account.AnnualRevenue); domain.getFieldByField(Account.Id, Account.Id); domain.getIdFieldByIdField(Account.Id, Account.Id); domain.getIdFieldsByIdField(Account.Id, Account.Id); @@ -101,6 +111,7 @@ private class fflib_SObjects2Test domain.getIdFieldValues(Account.Id, criteria); domain.getIntegerFieldValues(Account.NumberOfEmployees); domain.getIntegerFieldValues(Account.NumberOfEmployees, criteria); + domain.getDecimalFieldValues(Account.BillingLatitude, criteria); domain.getDoubleFieldValues(Account.BillingLatitude); domain.getDoubleFieldValues(Account.BillingLatitude, criteria); domain.getSObjectByIdField(Account.Id); @@ -327,6 +338,54 @@ private class fflib_SObjects2Test System.assert(domain.getDecimalFieldValues(Account.AnnualRevenue).contains((Decimal) 5)); } + @IsTest + static void itShouldSetFieldByIdField() + { + Id accountId = fflib_IDGenerator.generate(Account.SObjectType); + Account record = new Account(Id = accountId, Name = ''); + fflib_SObjects2 domain = new fflib_SObjects2(new List{ record }); + + domain.setFieldValue( + Schema.Account.Id, + Schema.Account.Name, + new Map + { + accountId => 'Hello' + } + ); + + System.assert(domain.getFirstSObject().get('Name') == 'Hello'); + } + + @IsTest + static void itShouldSetFieldByStringField() + { + Account record = new Account(Name = 'Hello', Rating = 'Cold'); + fflib_SObjects2 domain = new fflib_SObjects2(new List{ record }); + + domain.setFieldValue( + Schema.Account.Name, + Schema.Account.Rating, + new Map + { + 'Hello' => 'Warm' + } + ); + + System.assert(domain.getFirstSObject().get('Rating') == 'Warm'); + } + + @IsTest + static void itShouldSetField() + { + Account record = new Account(Name = 'Hello', Rating = 'Cold'); + fflib_SObjects2 domain = new fflib_SObjects2(new List{ record }); + + domain.setFieldValue(Account.Name, 'Hello World', new fflib_Criteria().equalTo(Account.Rating, 'Cold')); + + System.assert(domain.getFirstSObject().get('Name') == 'Hello World'); + } + private static DomainAccounts generateDomain() { DomainAccounts domain = new DomainAccounts(