Skip to content

Commit 9c13430

Browse files
authored
release/v3.4.0 (#135)
* Cannot modify a collection while it is being iterated Fix (#133) * 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]> * toId() (#139) Signed-off-by: Piotr PG Gajek <[email protected]> --------- Signed-off-by: Piotr PG Gajek <[email protected]>
1 parent 5fc5b87 commit 9c13430

File tree

5 files changed

+60
-12
lines changed

5 files changed

+60
-12
lines changed

force-app/main/default/classes/SOQL.cls

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ public virtual inherited sharing class SOQL implements Queryable {
132132
Queryable byIds(List<SObject> records);
133133
Queryable byRecordType(String recordTypeDeveloperName);
134134
// RESULT
135+
Id toId();
135136
Boolean doExist();
136137
String toString();
137138
Object toValueOf(SObjectField fieldToExtract);
@@ -781,6 +782,11 @@ public virtual inherited sharing class SOQL implements Queryable {
781782
return binder.getBindingMap();
782783
}
783784

785+
public Id toId() {
786+
builder.fields.clearAllFields(); // other fields not needed
787+
return toObject()?.Id;
788+
}
789+
784790
public Boolean doExist() {
785791
builder.fields.clearAllFields(); // other fields not needed
786792
return toList().size() > 0;
@@ -1119,7 +1125,9 @@ public virtual inherited sharing class SOQL implements Queryable {
11191125
if (!groupedFields.isEmpty() || !aggregateFunctions.isEmpty()) {
11201126
List<String> selectFields = new List<String>();
11211127

1122-
removeNotGroupedFields();
1128+
// To avoid "Field must be grouped or aggregated" error
1129+
// retain only grouped or aggregated fields
1130+
fields.retainAll(groupedFields);
11231131

11241132
selectFields.addAll(fields);
11251133
selectFields.addAll(aggregateFunctions);
@@ -1129,15 +1137,6 @@ public virtual inherited sharing class SOQL implements Queryable {
11291137

11301138
return 'SELECT ' + String.join(fields, ', ');
11311139
}
1132-
1133-
public void removeNotGroupedFields() {
1134-
// To avoid "Field must be grouped or aggregated" error
1135-
for (String field : fields) {
1136-
if (!groupedFields.contains(field)) {
1137-
fields.remove(field);
1138-
}
1139-
}
1140-
}
11411140
}
11421141

11431142
private class SoqlSubQuery implements SubQuery {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3-
<apiVersion>61.0</apiVersion>
3+
<apiVersion>62.0</apiVersion>
44
<status>Active</status>
55
</ApexClass>

force-app/main/default/classes/SOQL_Test.cls

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1965,6 +1965,20 @@ private class SOQL_Test {
19651965
Assert.areEqual('SELECT LeadSource FROM Lead GROUP BY LeadSource', soql);
19661966
}
19671967

1968+
@IsTest
1969+
static void groupByWithDefaultFieldsAndAggregateFunction() {
1970+
// Test
1971+
String soql = SOQL.of(Lead.SObjectType)
1972+
.with(Lead.FirstName, Lead.LastName, Lead.Email)
1973+
.count(Lead.Name)
1974+
.with(Lead.LeadSource)
1975+
.groupBy(Lead.LeadSource)
1976+
.toString();
1977+
1978+
// Verify
1979+
Assert.areEqual('SELECT LeadSource, COUNT(Name) FROM Lead GROUP BY LeadSource', soql);
1980+
}
1981+
19681982
@IsTest
19691983
static void havingFilterWithSObjectField() {
19701984
// Test
@@ -3008,6 +3022,19 @@ private class SOQL_Test {
30083022
Assert.areEqual(2, result);
30093023
}
30103024

3025+
@IsTest
3026+
static void toId() {
3027+
// Setup
3028+
Account acc = new Account(Name = 'Test 1');
3029+
insert acc;
3030+
3031+
// Test
3032+
Id accountId = SOQL.of(Account.SObjectType).byId(acc).toId();
3033+
3034+
// Verify
3035+
Assert.areEqual(acc.Id, accountId);
3036+
}
3037+
30113038
@IsTest
30123039
static void doExist() {
30133040
// Setup
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3-
<apiVersion>61.0</apiVersion>
3+
<apiVersion>62.0</apiVersion>
44
<status>Active</status>
55
</ApexClass>

website/docs/api/soql.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ The following are methods for `SOQL`.
167167

168168
[**RESULT**](#result)
169169

170+
- [`toId()`](#toid)
170171
- [`doExist()`](#doexist)
171172
- [`toValueOf(SObjectField fieldToExtract)`](#tovalueof)
172173
- [`toValuesOf(SObjectField fieldToExtract)`](#tovaluesof)
@@ -2143,8 +2144,29 @@ SOQL.of(Account.SObjectType)
21432144

21442145
## RESULT
21452146

2147+
### toId
2148+
2149+
```apex
2150+
Id toId()
2151+
```
2152+
2153+
**Example**
2154+
2155+
```apex
2156+
Id adminProfileId = SOQL.of(Profile.SObjectType)
2157+
.whereAre(SOQL.Filter.name().equal('System Administrator'))
2158+
.toId();
2159+
2160+
new User (
2161+
// ...
2162+
ProfileId = adminProfileId
2163+
);
2164+
```
2165+
21462166
### doExist
21472167

2168+
**Signature**
2169+
21482170
```apex
21492171
Boolean doExist()
21502172
```

0 commit comments

Comments
 (0)