Skip to content

Commit

Permalink
Cannot modify a collection while it is being iterated Fix (#133)
Browse files Browse the repository at this point in the history
* Cannot modify a collection while it is being iterated Fix

Signed-off-by: Piotr PG Gajek <[email protected]>

* Code refactoring

Signed-off-by: Piotr PG Gajek <[email protected]>

* groupByWithDefaultFieldsAndAggregateFunction unit test

Signed-off-by: Piotr PG Gajek <[email protected]>

---------

Signed-off-by: Piotr PG Gajek <[email protected]>
  • Loading branch information
pgajek2 authored Dec 18, 2024
1 parent 5fc5b87 commit 3d79427
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
13 changes: 3 additions & 10 deletions force-app/main/default/classes/SOQL.cls
Original file line number Diff line number Diff line change
Expand Up @@ -1119,7 +1119,9 @@ public virtual inherited sharing class SOQL implements Queryable {
if (!groupedFields.isEmpty() || !aggregateFunctions.isEmpty()) {
List<String> selectFields = new List<String>();

removeNotGroupedFields();
// To avoid "Field must be grouped or aggregated" error
// retain only grouped or aggregated fields
fields.retainAll(groupedFields);

selectFields.addAll(fields);
selectFields.addAll(aggregateFunctions);
Expand All @@ -1129,15 +1131,6 @@ public virtual inherited sharing class SOQL implements Queryable {

return 'SELECT ' + String.join(fields, ', ');
}

public void removeNotGroupedFields() {
// To avoid "Field must be grouped or aggregated" error
for (String field : fields) {
if (!groupedFields.contains(field)) {
fields.remove(field);
}
}
}
}

private class SoqlSubQuery implements SubQuery {
Expand Down
2 changes: 1 addition & 1 deletion force-app/main/default/classes/SOQL.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>61.0</apiVersion>
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>
14 changes: 14 additions & 0 deletions force-app/main/default/classes/SOQL_Test.cls
Original file line number Diff line number Diff line change
Expand Up @@ -1965,6 +1965,20 @@ private class SOQL_Test {
Assert.areEqual('SELECT LeadSource FROM Lead GROUP BY LeadSource', soql);
}

@IsTest
static void groupByWithDefaultFieldsAndAggregateFunction() {
// Test
String soql = SOQL.of(Lead.SObjectType)
.with(Lead.FirstName, Lead.LastName, Lead.Email)
.count(Lead.Name)
.with(Lead.LeadSource)
.groupBy(Lead.LeadSource)
.toString();

// Verify
Assert.areEqual('SELECT LeadSource, COUNT(Name) FROM Lead GROUP BY LeadSource', soql);
}

@IsTest
static void havingFilterWithSObjectField() {
// Test
Expand Down
2 changes: 1 addition & 1 deletion force-app/main/default/classes/SOQL_Test.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>61.0</apiVersion>
<apiVersion>62.0</apiVersion>
<status>Active</status>
</ApexClass>

0 comments on commit 3d79427

Please sign in to comment.