Skip to content

Commit 86e6555

Browse files
committed
More overload method for setFieldValue
Now it can consume a Map<Id, *> and Map<String, *> and includes unit-test Replaces PR: apex-enterprise-patterns#390
1 parent 1e5eb56 commit 86e6555

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

sfdx-source/apex-common/main/classes/fflib_SObjects.cls

+46-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,6 @@ public virtual class fflib_SObjects
257257
return result;
258258
}
259259

260-
261260
protected virtual void setFieldValue(Schema.SObjectField sObjectField, Object value)
262261
{
263262
for (SObject record : getRecords())
@@ -266,6 +265,52 @@ public virtual class fflib_SObjects
266265
}
267266
}
268267

268+
/**
269+
* Sets a value to the given field only when key field Id value is provided in the given map
270+
*
271+
* @param sObjectIdFieldToCheck The SObject Id Field to match the key against in the provided map
272+
* @param sObjectFieldToUpdate The SObjectField to store the mapped value when the key matches the value in the sObjectFieldToUpdate field
273+
* @param values Map of values to store by the sObjectIdFieldToCheck fields value
274+
*/
275+
@TestVisible
276+
protected virtual void setFieldValue(
277+
Schema.SObjectField sObjectIdFieldToCheck,
278+
Schema.SObjectField sObjectFieldToUpdate,
279+
Map<Id, Object> values)
280+
{
281+
for (SObject record : getRecords())
282+
{
283+
Id keyValue = (Id) record.get(sObjectIdFieldToCheck);
284+
if (values?.containsKey(keyValue))
285+
{
286+
record.put(sObjectFieldToUpdate, values.get(keyValue));
287+
}
288+
}
289+
}
290+
291+
/**
292+
* Sets a value to the given field only when key field String value is provided in the given map
293+
*
294+
* @param sObjectStringFieldToCheck The SObject String Field to match the key against in the provided map
295+
* @param sObjectFieldToUpdate The SObjectField to store the mapped value when the key matches the value in the sObjectFieldToUpdate field
296+
* @param values Map of values to store by the sObjectIdFieldToCheck fields value
297+
*/
298+
@TestVisible
299+
protected virtual void setFieldValue(
300+
Schema.SObjectField sObjectStringFieldToCheck,
301+
Schema.SObjectField sObjectFieldToUpdate,
302+
Map<String, Object> values)
303+
{
304+
for (SObject record : getRecords())
305+
{
306+
String keyValue = (String) record.get(sObjectStringFieldToCheck);
307+
if (values?.containsKey(keyValue))
308+
{
309+
record.put(sObjectFieldToUpdate, values.get(keyValue));
310+
}
311+
}
312+
}
313+
269314
/**
270315
* @param sObjectFieldToCheck The SObjectField to match the key against in the provided map
271316
* @param sObjectFieldToUpdate The SObjectField to store the mapped value when the key matches the value in the sObjectFieldToUpdate field

sfdx-source/apex-common/test/classes/fflib_SObjectsTest.cls

+40
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,46 @@ private class fflib_SObjectsTest
110110
System.assert(domain.selectByRating('Hot').size() == 1);
111111
}
112112

113+
@IsTest
114+
static void itShouldSetFieldByIdField()
115+
{
116+
final Id accountId = fflib_IDGenerator.generate(Account.SObjectType);
117+
Account record = new Account(Id = accountId, Name = '');
118+
fflib_SObjects domain = new fflib_SObjects(new List<SObject>{ record });
119+
120+
final String accountName = 'Hello';
121+
domain.setFieldValue(
122+
Schema.Account.Id,
123+
Schema.Account.Name,
124+
new Map<Id, String>
125+
{
126+
accountId => accountName
127+
}
128+
);
129+
130+
System.assertEquals(accountName, domain.getRecords().get(0).get(Schema.Account.Name));
131+
}
132+
133+
@IsTest
134+
static void itShouldSetFieldByStringField()
135+
{
136+
Account record = new Account(Name = 'Hello', Rating = 'Cold');
137+
fflib_SObjects domain = new fflib_SObjects(new List<SObject>{ record });
138+
139+
final String accountName = 'Hello';
140+
final String accountRating = 'Warm';
141+
domain.setFieldValue(
142+
Schema.Account.Name,
143+
Schema.Account.Rating,
144+
new Map<String, String>
145+
{
146+
accountName => accountRating
147+
}
148+
);
149+
150+
System.assertEquals(accountRating, domain.getRecords().get(0).get(Schema.Account.Rating));
151+
}
152+
113153
@IsTest
114154
static void testErrorLogging()
115155
{

0 commit comments

Comments
 (0)