diff --git a/force-app/main/default/classes/SOQL.cls b/force-app/main/default/classes/SOQL.cls index 7ed6810..8eac08f 100644 --- a/force-app/main/default/classes/SOQL.cls +++ b/force-app/main/default/classes/SOQL.cls @@ -132,6 +132,7 @@ public virtual inherited sharing class SOQL implements Queryable { Queryable byIds(List records); Queryable byRecordType(String recordTypeDeveloperName); // RESULT + Id toId(); Boolean doExist(); String toString(); Object toValueOf(SObjectField fieldToExtract); @@ -781,6 +782,11 @@ public virtual inherited sharing class SOQL implements Queryable { return binder.getBindingMap(); } + public Id toId() { + builder.fields.clearAllFields(); // other fields not needed + return toObject()?.Id; + } + public Boolean doExist() { builder.fields.clearAllFields(); // other fields not needed return toList().size() > 0; @@ -1119,7 +1125,9 @@ public virtual inherited sharing class SOQL implements Queryable { if (!groupedFields.isEmpty() || !aggregateFunctions.isEmpty()) { List selectFields = new List(); - 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); @@ -1129,15 +1137,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 { diff --git a/force-app/main/default/classes/SOQL.cls-meta.xml b/force-app/main/default/classes/SOQL.cls-meta.xml index 651b172..998805a 100644 --- a/force-app/main/default/classes/SOQL.cls-meta.xml +++ b/force-app/main/default/classes/SOQL.cls-meta.xml @@ -1,5 +1,5 @@ - 61.0 + 62.0 Active diff --git a/force-app/main/default/classes/SOQL_Test.cls b/force-app/main/default/classes/SOQL_Test.cls index 3d9a31f..d4d9850 100644 --- a/force-app/main/default/classes/SOQL_Test.cls +++ b/force-app/main/default/classes/SOQL_Test.cls @@ -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 @@ -3008,6 +3022,19 @@ private class SOQL_Test { Assert.areEqual(2, result); } + @IsTest + static void toId() { + // Setup + Account acc = new Account(Name = 'Test 1'); + insert acc; + + // Test + Id accountId = SOQL.of(Account.SObjectType).byId(acc).toId(); + + // Verify + Assert.areEqual(acc.Id, accountId); + } + @IsTest static void doExist() { // Setup diff --git a/force-app/main/default/classes/SOQL_Test.cls-meta.xml b/force-app/main/default/classes/SOQL_Test.cls-meta.xml index 651b172..998805a 100644 --- a/force-app/main/default/classes/SOQL_Test.cls-meta.xml +++ b/force-app/main/default/classes/SOQL_Test.cls-meta.xml @@ -1,5 +1,5 @@ - 61.0 + 62.0 Active diff --git a/website/docs/api/soql.md b/website/docs/api/soql.md index 68f6f9f..e1bb031 100644 --- a/website/docs/api/soql.md +++ b/website/docs/api/soql.md @@ -167,6 +167,7 @@ The following are methods for `SOQL`. [**RESULT**](#result) +- [`toId()`](#toid) - [`doExist()`](#doexist) - [`toValueOf(SObjectField fieldToExtract)`](#tovalueof) - [`toValuesOf(SObjectField fieldToExtract)`](#tovaluesof) @@ -2143,8 +2144,29 @@ SOQL.of(Account.SObjectType) ## RESULT +### toId + +```apex +Id toId() +``` + +**Example** + +```apex +Id adminProfileId = SOQL.of(Profile.SObjectType) + .whereAre(SOQL.Filter.name().equal('System Administrator')) + .toId(); + +new User ( + // ... + ProfileId = adminProfileId +); +``` + ### doExist +**Signature** + ```apex Boolean doExist() ```