From f337bf279c53fce493478e939866db0625f74b65 Mon Sep 17 00:00:00 2001 From: Ilija Pavlic Date: Sat, 30 Oct 2021 12:23:29 -0400 Subject: [PATCH] Fix runtime list conversion bug --- .../classes/collection/ObjectCollection.cls | 12 ++++--- .../collection/SObjectCollectionTest.cls | 2 +- .../docs/api/collection/ObjectCollection.md | 32 +++++++++++++++++++ .../docs/api/collection/SObjectCollection.md | 2 +- 4 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 website/docs/api/collection/ObjectCollection.md diff --git a/sfdx-source/apex-fp/main/classes/collection/ObjectCollection.cls b/sfdx-source/apex-fp/main/classes/collection/ObjectCollection.cls index bf35e57..3a3a7a0 100644 --- a/sfdx-source/apex-fp/main/classes/collection/ObjectCollection.cls +++ b/sfdx-source/apex-fp/main/classes/collection/ObjectCollection.cls @@ -5,11 +5,15 @@ public with sharing class ObjectCollection { this.objects = objects; } - public List asList() { - return new List(objects); + public List asList(Type listType) { + List typedObjects = (List) listType.newInstance(); + typedObjects.addAll(objects); + return typedObjects; } - public Set asSet() { - return new Set(objects); + public Set asSet(Type setType) { + Set typedObjects = (Set) setType.newInstance(); + typedObjects.addAll(objects); + return typedObjects; } } diff --git a/sfdx-source/apex-fp/test/classes/collection/SObjectCollectionTest.cls b/sfdx-source/apex-fp/test/classes/collection/SObjectCollectionTest.cls index bc50cc1..66a3bf6 100644 --- a/sfdx-source/apex-fp/test/classes/collection/SObjectCollectionTest.cls +++ b/sfdx-source/apex-fp/test/classes/collection/SObjectCollectionTest.cls @@ -734,7 +734,7 @@ private class SObjectCollectionTest { .mapAll( Fn.MapTo(MappingTarget.class).mapField('name', Account.Name) ) - .asList(); + .asList(List.class); System.assertEquals(2, result.size()); System.assertEquals('foo', result[0].name); diff --git a/website/docs/api/collection/ObjectCollection.md b/website/docs/api/collection/ObjectCollection.md new file mode 100644 index 0000000..5679f99 --- /dev/null +++ b/website/docs/api/collection/ObjectCollection.md @@ -0,0 +1,32 @@ +# ObjectCollection + +## asList + +Returns objects in the collection as a list. + +**Signature** + +``` +List asList(Type listType) +``` + +**Example** +```apex title="Mapping to objects with MapTo function factory" +List actions = (List) SObjectCollection.of(opps) + .mapAll( + Fn.MapTo(OpportunityAction.class) + .mapField('oppId', Opportunity.Id) + .setField('action', 'follow up') + .setField('createdAt', Datetime.now()) + ).asList(List.class); +``` + +## asSet + +Returns objects in the collection as a set. + +**Signature** + +``` +Set asSet(Type listType) +``` \ No newline at end of file diff --git a/website/docs/api/collection/SObjectCollection.md b/website/docs/api/collection/SObjectCollection.md index e4ab866..c657978 100644 --- a/website/docs/api/collection/SObjectCollection.md +++ b/website/docs/api/collection/SObjectCollection.md @@ -320,7 +320,7 @@ List actions = (List) SObjectCollection.of .mapField('oppId', Opportunity.Id) .setField('action', 'follow up') .setField('createdAt', Datetime.now()) - ).asList(); + ).asList(List.class); ``` ## mapSome