Skip to content

Commit

Permalink
Row with null Id fix
Browse files Browse the repository at this point in the history
  • Loading branch information
pgajek2 committed Oct 27, 2023
1 parent 2bb5566 commit 29232ac
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion force-app/main/default/classes/SOQL.cls
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,11 @@ public virtual inherited sharing class SOQL implements Queryable {
public Set<String> toValuesOf(SObjectField fieldToExtract) {
// https://salesforce.stackexchange.com/questions/393308/get-a-list-of-one-column-from-a-soql-result
builder.fields.clearAllFields(); // other fields not needed
return new Map<String, SObject>(with(fieldToExtract, 'Id').groupBy(fieldToExtract).toAggregated()).keySet();
try {
return new Map<String, SObject>(with(fieldToExtract, 'Id').groupBy(fieldToExtract).toAggregated()).keySet();
} catch (ListException e) { // Row with null Id at index: 0
return new Set<String>();
}
}

public Integer toInteger() {
Expand Down
12 changes: 12 additions & 0 deletions force-app/main/default/classes/SOQL_Test.cls
Original file line number Diff line number Diff line change
Expand Up @@ -1775,6 +1775,18 @@ private class SOQL_Test {
Assert.areEqual(2, accountNames.size());
}

@IsTest
static void toValuesOfWhenNoValues() {
// Setup
insertAccounts();

// Test
Set<String> accountNames = SOQL.of(Account.SObjectType).toValuesOf(Account.Industry);

// Verify
Assert.areEqual(0, accountNames.size());
}

@IsTest
static void toValuesOfWithDefaultFields() {
// Setup
Expand Down

1 comment on commit 29232ac

@vercel
Copy link

@vercel vercel bot commented on 29232ac Oct 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.