Add getChangedFields to triggerContext (#65)

This commit is contained in:
William Velzeboer
2025-01-27 13:04:22 +00:00
committed by GitHub
parent 5bb4c0f1ad
commit 73682a1ffa
3 changed files with 71 additions and 3 deletions
+2 -2
View File
@@ -7,5 +7,5 @@ rm -Rf fflib-apex-common/
git clone https://github.com/apex-enterprise-patterns/fflib-apex-mocks.git git clone https://github.com/apex-enterprise-patterns/fflib-apex-mocks.git
git clone https://github.com/apex-enterprise-patterns/fflib-apex-common.git git clone https://github.com/apex-enterprise-patterns/fflib-apex-common.git
sfdx force:source:deploy -p ./fflib-apex-mocks/sfdx-source sfdx project deploy start -c --source-dir ./fflib-apex-mocks/sfdx-source
sfdx force:source:deploy -p ./fflib-apex-common/sfdx-source sfdx project deploy start -c --source-dir ./fflib-apex-common/sfdx-source
@@ -95,6 +95,41 @@ public virtual inherited sharing class fflib_TriggerContext
return triggerOperation; return triggerOperation;
} }
public Set<String> getChangedFields()
{
Set<String> result = new Set<String>();
for (SObject newRecord : getRecords())
{
Id recordId = (Id) newRecord.get('Id');
if (getExistingRecords() == null || !getExistingRecords().containsKey(recordId)) continue;
SObject oldRecord = getExistingRecords().get(recordId);
Map<String, Object> oldValuesByFieldName = oldRecord.getPopulatedFieldsAsMap();
Map<String, Object> newValuesByFieldName = newRecord.getPopulatedFieldsAsMap();
Set<String> fieldNames = new Set<String>();
fieldNames.addAll(oldValuesByFieldName.keySet());
fieldNames.addAll(newValuesByFieldName.keySet());
for (String fieldName : fieldNames)
{
if (oldValuesByFieldName.containsKey(fieldName) == false
&& newValuesByFieldName.containsKey(fieldName) == false) continue;
if ((oldValuesByFieldName.containsKey(fieldName) == false
&& newValuesByFieldName.containsKey(fieldName))
||
(oldValuesByFieldName.containsKey(fieldName)
&& newValuesByFieldName.containsKey(fieldName) == false)
||
(oldValuesByFieldName.get(fieldName) != newValuesByFieldName.get(fieldName))
)
{
result.add(fieldName);
}
}
}
return result;
}
/** /**
* Detects whether any values in context records have changed for given field as string * Detects whether any values in context records have changed for given field as string
* Returns list of SObject records that have changes in the specified fields * Returns list of SObject records that have changes in the specified fields
@@ -137,7 +137,40 @@ private class fflib_TriggerHandlerTest
// THEN it should only return the changed records // THEN it should only return the changed records
Map<Id, SObject> resultMap = new Map<Id, SObject>(result); Map<Id, SObject> resultMap = new Map<Id, SObject>(result);
System.Assert.areEqual(1, result.size()); System.Assert.areEqual(1, result.size());
System.Assert.istrue(resultMap.containsKey(idLuke)); System.Assert.isTrue(resultMap.containsKey(idLuke));
}
@IsTest
private static void itShouldReturnTheChangedFields()
{
// GIVEN a domain with old and changed records
Id idLuke = fflib_IDGenerator.generate(Account.SObjectType);
Id idHan = fflib_IDGenerator.generate(Account.SObjectType);
Id idLeia = fflib_IDGenerator.generate(Account.SObjectType);
List<Account> oldRecords = new List<Account>
{
new Account(Id = idLuke, Name = 'Luke', Description = 'Jedi'),
new Account(Id = idHan, Name = 'Han', Description = 'Pilot'),
new Account(Id = idLeia, Name = 'Leia')
};
List<Account> newRecords = oldRecords.deepClone(true, true, true);
newRecords.get(0).Name = 'Luke SkyWalker';
newRecords.get(0).Description = 'Jedi Master';
newRecords.get(1).Name = 'Han Solo';
newRecords.get(1).Description = null;
fflib_TriggerContext ctx = new fflib_TriggerContext(newRecords);
ctx.existingRecords = new Map<Id, SObject>(oldRecords);
// WHEN we create a domain with ExistingRecords and request the changed records
Set<String> result = ctx.getChangedFields();
// THEN it should only return the changed records
System.Assert.areEqual(2, result.size());
System.Assert.isTrue(result.contains('Name'));
System.Assert.isTrue(result.contains('Description'));
} }
@IsTest @IsTest