Skip to content

Instantly share code, notes, and snippets.

@aidan-harding
Last active August 23, 2023 12:49
Show Gist options
  • Save aidan-harding/268f0427c8127f729fff1a105e632230 to your computer and use it in GitHub Desktop.
Save aidan-harding/268f0427c8127f729fff1a105e632230 to your computer and use it in GitHub Desktop.
Probably a misuse of Nebula Core functional stuff, but interesting to try
/**
* @author [email protected]
* @date 22/08/2023
* @description For a given Long text field, when it changes append the old value to the new value with a new line (\n).
* If the field changes to blank/null then the old value remains.
*
* Example:
*
* Old Value New Value Expected Value
* --------------------------------------------
* null Text 1 Text 1
* --------------------------------------------
* Text 1 Text 2 Text 2
* Text 1
* --------------------------------------------
* Text 2 Text 3 Text 3
* Text 1 Text 2
* Text 1
* --------------------------------------------
* Text 3 null Text 3
* Text 2 Text 2
* Text 1 Text 1
*
*/
@IsTest
private class LazyConcatTest {
static List<Contact> oldList = new List<Contact>{
new Contact(),
new Contact(Description = 'Text 1'),
new Contact(Description = 'Text 2\nText 1'),
new Contact(Description = 'Text 3\nText 2\nText 1')
};
static List<Contact> newList = new List<Contact>{
new Contact(Description = 'Text 1'),
new Contact(Description = 'Text 2'),
new Contact(Description = 'Text 3'),
new Contact()
};
@IsTest
static void twoIterators() {
nebc.LazyIterator oldDescriptionsIterator = new nebc.LazyTriggerContextPairIterator(oldList, newList)
.filterT(new nebc.IsFieldChangedInTrigger(Contact.Description))
.filterT(new nebc.OldFromTriggerContextPair(), new nebc.IsNotNull(Contact.Description))
.oldRecords()
.get(Contact.Description);
new nebc.LazyTriggerContextPairIterator(oldList, newList)
.filterT(new nebc.IsFieldChangedInTrigger(Contact.Description))
.filterT(new nebc.OldFromTriggerContextPair(), new nebc.IsNotNull(Contact.Description))
.newRecords()
.putIf(new nebc.IsNotNull(Contact.Description), Contact.Description, new nebc.Add(new nebc.FieldFromSObject(Contact.Description), '\n').add(oldDescriptionsIterator))
.putIf(new nebc.IsNull(Contact.Description), Contact.Description, oldDescriptionsIterator)
.forEach();
Assert.isTrue(makeAssertions());
}
@IsTest
static void tuples() {
new nebc.LazyTriggerContextPairIterator(oldList, newList)
.filterT(new nebc.IsFieldChangedInTrigger(Contact.Description))
.filterT(new nebc.OldFromTriggerContextPair(), new nebc.IsNotNull(Contact.Description))
.mapValues(new nebc.TupleMapFunction(
new nebc.IfThen(
new nebc.IsNotNull(new nebc.Composition(new nebc.NewFromTriggerContextPair()).compose(new nebc.FieldFromSObject(Contact.Description))),
new nebc.Add(
new nebc.Composition(new nebc.NewFromTriggerContextPair()).compose(new nebc.FieldFromSObject(Contact.Description)),
'\n'
)
.add(new nebc.Composition(new nebc.OldFromTriggerContextPair()).compose(new nebc.FieldFromSObject(Contact.Description))))
.elseFunction(new nebc.Composition(new nebc.OldFromTriggerContextPair()).compose(new nebc.FieldFromSObject(Contact.Description))),
new nebc.NewFromTriggerContextPair()))
.mapValues(new nebc.Swap())
.forEach(new nebc.SObjectPutField(Contact.Description, new nebc.IdentityFunction()));
Assert.isTrue(makeAssertions());
}
static Boolean makeAssertions() {
Assert.areEqual('Text 1', newList[0].Description);
Assert.areEqual('Text 2\nText 1', newList[1].Description);
Assert.areEqual('Text 3\nText 2\nText 1', newList[2].Description);
Assert.areEqual('Text 3\nText 2\nText 1', newList[3].Description);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment