Skip to content

Commit

Permalink
toMap methods
Browse files Browse the repository at this point in the history
  • Loading branch information
pgajek2 committed Oct 9, 2023
1 parent fd7b31e commit 6387c51
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 2 deletions.
39 changes: 39 additions & 0 deletions force-app/main/default/classes/SOQL.cls
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ public virtual inherited sharing class SOQL implements Queryable {
List<AggregateResult> toAggregated();
Map<Id, SObject> toMap();
Map<String, SObject> toMap(SObjectField keyField);
Map<String, String> toMap(SObjectField keyField, SObjectField valueField);
Map<String, List<SObject>> toAggregatedMap(SObjectField keyField);
Map<String, List<String>> toAggregatedMap(SObjectField keyField, SObjectField valueField);
Database.QueryLocator toQueryLocator();
}

Expand Down Expand Up @@ -588,6 +590,23 @@ public virtual inherited sharing class SOQL implements Queryable {
return cutomKeyToRecord;
}

public Map<String, String> toMap(SObjectField keyField, SObjectField valueField) {
builder.fields.clearAllFields(); // other fields not needed

with(keyField, valueField);

Map<String, String> cutomKeyToCustomValue = new Map<String, String>();

for (SObject record : toList()) {
cutomKeyToCustomValue.put(
String.valueOf(record.get(keyField)),
String.valueOf(record.get(valueField))
);
}

return cutomKeyToCustomValue;
}

public Map<String, List<SObject>> toAggregatedMap(SObjectField keyField) {
with(keyField);

Expand All @@ -606,6 +625,26 @@ public virtual inherited sharing class SOQL implements Queryable {
return cutomKeyToRecords;
}

public Map<String, List<String>> toAggregatedMap(SObjectField keyField, SObjectField valueField) {
builder.fields.clearAllFields(); // other fields not needed

with(keyField, valueField);

Map<String, List<String>> customKeyToValues = new Map<String, List<String>>();

for (SObject record : toList()) {
String customKey = String.valueOf(record.get(keyField));

if (!customKeyToValues.containsKey(customKey)) {
customKeyToValues.put(customKey, new List<String>());
}

customKeyToValues.get(customKey).add(String.valueOf(record.get(valueField)));
}

return customKeyToValues;
}

public Database.QueryLocator toQueryLocator() {
return executor.toQueryLocator(builder.toString(), binder.getBindingMap());
}
Expand Down
34 changes: 33 additions & 1 deletion force-app/main/default/classes/SOQL_Test.cls
Original file line number Diff line number Diff line change
Expand Up @@ -1921,6 +1921,22 @@ private class SOQL_Test {
}
}

@IsTest
static void toMapWithCustomKeyAndCustomValue() {
// Setup
List<Account> accounts = insertAccounts();

// Test
Map<String, String> result = SOQL.of(Account.SObjectType).toMap(Account.Name, Account.Id);

// Verify
Assert.areEqual(accounts.size(), result.size());

for (Account acc : accounts) {
Assert.isNotNull(result.get(acc.Name));
}
}

@IsTest
static void toAggregatedMapWithCustomKey() {
// Setup
Expand All @@ -1933,7 +1949,7 @@ private class SOQL_Test {
Assert.areEqual(accounts.size(), result.size());

for (Account acc : accounts) {
Assert.isNotNull(result.get(acc.Name));
Assert.isFalse(result.get(acc.Name).isEmpty());
}
}

Expand All @@ -1949,6 +1965,22 @@ private class SOQL_Test {
Assert.areEqual(1, result.size()); // grouped by empty Industry
}

@IsTest
static void toAggregatedMapWithCustomKeyAndCustomValue() {
// Setup
List<Account> accounts = insertAccounts();

// Test
Map<String, List<String>> result = SOQL.of(Account.SObjectType).toAggregatedMap(Account.Name, Account.Id);

// Verify
Assert.areEqual(accounts.size(), result.size());

for (Account acc : accounts) {
Assert.isFalse(result.get(acc.Name).isEmpty());
}
}

@IsTest
static void toQueryLocator() {
// Test
Expand Down
33 changes: 32 additions & 1 deletion website/docs/api/soql.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ The following are methods for `SOQL`.
- [`toAggregated()`](#toaggregated)
- [`toMap()`](#tomap)
- [`toMap(SObjectField keyField)`](#tomap-with-custom-key)
- [`toMap(SObjectField keyField, SObjectField valueField)`](#tomap-with-custom-key-and-value)
- [`toAggregatedMap(SObjectField keyField)`](#toaggregatedmap)
- [`toAggregatedMap(SObjectField keyField, SObjectField valueField)`](#toaggregatedmap-with-custom-value)
- [`toQueryLocator()`](#toquerylocator)

## INIT
Expand Down Expand Up @@ -1479,7 +1481,7 @@ Map<Id, SObject> toMap()
**Example**

```apex
SOQL.of(Account.SObjectType).toMap();
Map<Id, Account> idToAccount = (Map<Id, Account>) SOQL.of(Account.SObjectType).toMap();
```

### toMap with custom key
Expand All @@ -1496,6 +1498,21 @@ Map<String, SObject> toMap(SObjectField keyField)
Map<String, Account> nameToAccount = (Map<String, Account>) SOQL.of(Account.SObjectType).toMap(Account.Name);
```

### toMap with custom key and value

**Signature**

```apex
Map<String, String> toMap(SObjectField keyField, , SObjectField valueField)
```

**Example**

```apex
Map<String, String> nameToAccount = SOQL.of(Account.SObjectType).toMap(Account.Name, Account.Industry);
```


### toAggregatedMap

**Signature**
Expand All @@ -1510,6 +1527,20 @@ Map<String, List<SObject>> toAggregatedMap(SObjectField keyField)
Map<String, Account> industryToAccounts = SOQL.of(Account.SObjectType).toAggregatedMap(Account.Industry);
```

### toAggregatedMap with custom value

**Signature**

```apex
Map<String, List<String>> toAggregatedMap(SObjectField keyField, SObjectField valueField)
```

**Example**

```apex
Map<String, List<String>> industryToAccounts = SOQL.of(Account.SObjectType).toAggregatedMap(Account.Industry, Account.Name);
```

### toQueryLocator

**Signature**
Expand Down

0 comments on commit 6387c51

Please sign in to comment.