diff --git a/README.md b/README.md index 4ba4ee4..0448ce1 100644 --- a/README.md +++ b/README.md @@ -220,6 +220,11 @@ Collection picked = Collection.of(opportunities).pick(new Set{'Name', 'A Maps all elements of `Collection` view into another `Collection` view with the provided `SObjectToSObjectFunction` mapping function. +| Modifier and type | Method | Description | +|-------------------|--------|-------------| +| `Collection` | `mapAll(SObjectToSObjectFunction fn)` | Returns a new `Collection` view formed by mapping all current view elements with `fn` | + + ```apex private class DoubleAmount implements SObjectToSObjectFunction { public SObject apply(SObject record) { @@ -236,9 +241,16 @@ List opps = new List{ Collection.of(opps).mapAll(new DoubleAmount()); // amounts have been doubled ``` -| Modifier and type | Method | Description | -|-------------------|--------|-------------| -| `Collection` | `mapAll(SObjectToSObjectFunction fn)` | Returns a new `Collection` view formed by mapping all current view elements with `fn` | +One `SObjectToSObjectFunction` is provided out of the box, `RecordTransform`. It is instantiated through a factory method on `Transform`: + +#### `RecordTransform` + +`RecordTransform` copies all defined fields from `prototype` record to the record it is applied to. Values of fields defined for `prototype` are overwritten on +target records. Other fields on target record are not modified. + +```apex +Collection.of(opps).mapAll(Transform.record(new Opportunity(Name = 'Test'))); // Name field has been overwritten with 'Test' +``` ### `mapSome` diff --git a/src/classes/CollectionTest.cls b/src/classes/CollectionTest.cls index 22e8a57..f194af0 100644 --- a/src/classes/CollectionTest.cls +++ b/src/classes/CollectionTest.cls @@ -458,4 +458,14 @@ public class CollectionTest { System.assertEquals(200, opportunities[0].Amount); System.assertEquals(150, opportunities[1].Amount); } + + @IsTest + static void testMapWithTransform() { + List opportunities = Collection.of(new List{ + new Opportunity(Amount = 100), + new Opportunity(Amount = 150) + }).mapAll(Transform.record(new Opportunity(Amount = 123))).asList(); + System.assertEquals(123, opportunities[0].Amount); + System.assertEquals(123, opportunities[1].Amount); + } } diff --git a/src/classes/RecordTransform.cls b/src/classes/RecordTransform.cls new file mode 100644 index 0000000..3333e7a --- /dev/null +++ b/src/classes/RecordTransform.cls @@ -0,0 +1,17 @@ +public class RecordTransform implements SObjectToSObjectFunction { + private SObject prototype; + private Map populatedFieldsMap; + + public SObject apply(SObject record) { + + for (String field : populatedFieldsMap.keySet()) { + record.put(field, prototype.get(field)); + } + return record; + } + + public RecordTransform(sObject prototype) { + this.prototype = prototype; + this.populatedFieldsMap = prototype.getPopulatedFieldsAsMap(); + } +} diff --git a/src/classes/RecordTransform.cls-meta.xml b/src/classes/RecordTransform.cls-meta.xml new file mode 100644 index 0000000..45aa0a0 --- /dev/null +++ b/src/classes/RecordTransform.cls-meta.xml @@ -0,0 +1,5 @@ + + + 44.0 + Active + diff --git a/src/classes/RecordTransformTest.cls b/src/classes/RecordTransformTest.cls new file mode 100644 index 0000000..b374deb --- /dev/null +++ b/src/classes/RecordTransformTest.cls @@ -0,0 +1,19 @@ +@IsTest +public class RecordTransformTest { + @IsTest + public static void definedFieldsAreUsed() { + Opportunity opp = new Opportunity(); + RecordTransform t = Transform.record(new Opportunity(Amount = 1000, Name = 'Test')); + t.apply(opp); + System.assertEquals(1000, opp.Amount); + System.assertEquals('Test', opp.Name); + } + + @IsTest + public static void fieldsAreOverwritten() { + Opportunity opp = new Opportunity(Amount = 1000); + RecordTransform t = Transform.record(new Opportunity(Amount = 2000)); + t.apply(opp); + System.assertEquals(2000, opp.Amount); + } +} diff --git a/src/classes/RecordTransformTest.cls-meta.xml b/src/classes/RecordTransformTest.cls-meta.xml new file mode 100644 index 0000000..45aa0a0 --- /dev/null +++ b/src/classes/RecordTransformTest.cls-meta.xml @@ -0,0 +1,5 @@ + + + 44.0 + Active + diff --git a/src/classes/Transform.cls b/src/classes/Transform.cls new file mode 100644 index 0000000..ca050d0 --- /dev/null +++ b/src/classes/Transform.cls @@ -0,0 +1,5 @@ +public with sharing class Transform { + public static RecordTransform record(SObject record) { + return new RecordTransform(record); + } +} diff --git a/src/classes/Transform.cls-meta.xml b/src/classes/Transform.cls-meta.xml new file mode 100644 index 0000000..45aa0a0 --- /dev/null +++ b/src/classes/Transform.cls-meta.xml @@ -0,0 +1,5 @@ + + + 44.0 + Active +