Skip to content

Commit ce11093

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 ce11093

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

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

+46
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,52 @@ public virtual class fflib_SObjects
266266
}
267267
}
268268

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