Skip to content

Commit

Permalink
Fix runtime list conversion bug
Browse files Browse the repository at this point in the history
ipavlic committed Oct 30, 2021
1 parent 94b2d36 commit f337bf2
Showing 4 changed files with 42 additions and 6 deletions.
12 changes: 8 additions & 4 deletions sfdx-source/apex-fp/main/classes/collection/ObjectCollection.cls
Original file line number Diff line number Diff line change
@@ -5,11 +5,15 @@ public with sharing class ObjectCollection {
this.objects = objects;
}

public List<Object> asList() {
return new List<Object>(objects);
public List<Object> asList(Type listType) {
List<Object> typedObjects = (List<Object>) listType.newInstance();
typedObjects.addAll(objects);
return typedObjects;
}

public Set<Object> asSet() {
return new Set<Object>(objects);
public Set<Object> asSet(Type setType) {
Set<Object> typedObjects = (Set<Object>) setType.newInstance();
typedObjects.addAll(objects);
return typedObjects;
}
}
Original file line number Diff line number Diff line change
@@ -734,7 +734,7 @@ private class SObjectCollectionTest {
.mapAll(
Fn.MapTo(MappingTarget.class).mapField('name', Account.Name)
)
.asList();
.asList(List<MappingTarget>.class);

System.assertEquals(2, result.size());
System.assertEquals('foo', result[0].name);
32 changes: 32 additions & 0 deletions website/docs/api/collection/ObjectCollection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# ObjectCollection

## asList

Returns objects in the collection as a list.

**Signature**

```
List<Object> asList(Type listType)
```

**Example**
```apex title="Mapping to objects with MapTo function factory"
List<OpportunityAction> actions = (List<OpportunityAction>) SObjectCollection.of(opps)
.mapAll(
Fn.MapTo(OpportunityAction.class)
.mapField('oppId', Opportunity.Id)
.setField('action', 'follow up')
.setField('createdAt', Datetime.now())
).asList(List<OpportunityAction>.class);
```

## asSet

Returns objects in the collection as a set.

**Signature**

```
Set<Object> asSet(Type listType)
```
2 changes: 1 addition & 1 deletion website/docs/api/collection/SObjectCollection.md
Original file line number Diff line number Diff line change
@@ -320,7 +320,7 @@ List<OpportunityAction> actions = (List<OpportunityAction>) SObjectCollection.of
.mapField('oppId', Opportunity.Id)
.setField('action', 'follow up')
.setField('createdAt', Datetime.now())
).asList();
).asList(List<OpportunityAction>.class);
```

## mapSome

0 comments on commit f337bf2

Please sign in to comment.