Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More overload method for setFieldValue #404

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions sfdx-source/apex-common/main/classes/fflib_SObjects.cls
Original file line number Diff line number Diff line change
Expand Up @@ -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<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));
}
}
}

/**
* @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
Expand Down
40 changes: 40 additions & 0 deletions sfdx-source/apex-common/test/classes/fflib_SObjectsTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -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<SObject>{ record });

final String accountName = 'Hello';
domain.setFieldValue(
Schema.Account.Id,
Schema.Account.Name,
new Map<Id, String>
{
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<SObject>{ record });

final String accountName = 'Hello';
final String accountRating = 'Warm';
domain.setFieldValue(
Schema.Account.Name,
Schema.Account.Rating,
new Map<String, String>
{
accountName => accountRating
}
);

System.assertEquals(accountRating, domain.getRecords().get(0).get(Schema.Account.Rating));
}

@IsTest
static void testDomainErrorLogging()
{
Expand Down