Add setter method to fflib_SObjects2 plus unit-test

Signed-off-by: WimVelzeboer <wimvelzeboer@protonmail.com>
This commit is contained in:
WimVelzeboer
2021-11-04 17:18:46 +00:00
parent 6eee1acdb6
commit 1f45c8d4c2
2 changed files with 120 additions and 4 deletions
@@ -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<Id, Object> 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<String, Object> values)
{
for (SObject record : getRecords())
{
String keyValue = (String) record.get(sObjectStringFieldToCheck);
if (values?.containsKey(keyValue))
{
record.put(sObjectFieldToUpdate, values.get(keyValue));
}
}
}
}