diff --git a/sfdx-source/apex-common/main/classes/fflib_SObjects.cls b/sfdx-source/apex-common/main/classes/fflib_SObjects.cls index 95c48ccf017..226bea9f4a9 100644 --- a/sfdx-source/apex-common/main/classes/fflib_SObjects.cls +++ b/sfdx-source/apex-common/main/classes/fflib_SObjects.cls @@ -362,6 +362,52 @@ public virtual class fflib_SObjects } } + /** + * 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)); + } + } + } + /** * @param fieldToCheck The SObjectField to match the key against in the provided map * @param fieldToUpdate The SObjectField to store the mapped value when the key matches the value in the fieldToUpdate field diff --git a/sfdx-source/apex-common/test/classes/fflib_SObjectsTest.cls b/sfdx-source/apex-common/test/classes/fflib_SObjectsTest.cls index dd5bd13f0a0..24a52066271 100644 --- a/sfdx-source/apex-common/test/classes/fflib_SObjectsTest.cls +++ b/sfdx-source/apex-common/test/classes/fflib_SObjectsTest.cls @@ -176,6 +176,46 @@ private class fflib_SObjectsTest System.assert(domain.selectByRating('Hot').size() == 1); } + @IsTest + static void itShouldSetFieldByIdField() + { + final Id accountId = fflib_IDGenerator.generate(Account.SObjectType); + Account record = new Account(Id = accountId, Name = ''); + fflib_SObjects domain = new fflib_SObjects(new List{ record }); + + final String accountName = 'Hello'; + domain.setFieldValue( + Schema.Account.Id, + Schema.Account.Name, + new Map + { + accountId => accountName + } + ); + + System.assertEquals(accountName, domain.getRecords().get(0).get(Schema.Account.Name)); + } + + @IsTest + static void itShouldSetFieldByStringField() + { + Account record = new Account(Name = 'Hello', Rating = 'Cold'); + fflib_SObjects domain = new fflib_SObjects(new List{ record }); + + final String accountName = 'Hello'; + final String accountRating = 'Warm'; + domain.setFieldValue( + Schema.Account.Name, + Schema.Account.Rating, + new Map + { + accountName => accountRating + } + ); + + System.assertEquals(accountRating, domain.getRecords().get(0).get(Schema.Account.Rating)); + } + @IsTest static void testDomainErrorLogging() {