From ce110935dfb0bb3fbd66927fd80af6eab51d8209 Mon Sep 17 00:00:00 2001 From: wimvelzeboer Date: Wed, 6 Apr 2022 09:02:33 +0100 Subject: [PATCH] More overload method for setFieldValue Now it can consume a Map and Map and includes unit-test Replaces PR: #390 --- .../main/classes/fflib_SObjects.cls | 46 +++++++++++++++++++ .../test/classes/fflib_SObjectsTest.cls | 40 ++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/sfdx-source/apex-common/main/classes/fflib_SObjects.cls b/sfdx-source/apex-common/main/classes/fflib_SObjects.cls index 92089ace871..3d5b3ea56dc 100644 --- a/sfdx-source/apex-common/main/classes/fflib_SObjects.cls +++ b/sfdx-source/apex-common/main/classes/fflib_SObjects.cls @@ -266,6 +266,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 sObjectFieldToCheck The SObjectField 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 diff --git a/sfdx-source/apex-common/test/classes/fflib_SObjectsTest.cls b/sfdx-source/apex-common/test/classes/fflib_SObjectsTest.cls index d9dcd756b1d..b670c0f4897 100644 --- a/sfdx-source/apex-common/test/classes/fflib_SObjectsTest.cls +++ b/sfdx-source/apex-common/test/classes/fflib_SObjectsTest.cls @@ -110,6 +110,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 testErrorLogging() {