From 68ebda59a8fc868ffcb33b18f2f07691dd7c0244 Mon Sep 17 00:00:00 2001 From: Piotr PG Gajek Date: Tue, 26 Sep 2023 16:51:14 +0200 Subject: [PATCH] groupByWithDefaultFields --- force-app/main/default/classes/SOQL.cls | 1 + force-app/main/default/classes/SOQL_Test.cls | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/force-app/main/default/classes/SOQL.cls b/force-app/main/default/classes/SOQL.cls index cb29732..0044e1e 100644 --- a/force-app/main/default/classes/SOQL.cls +++ b/force-app/main/default/classes/SOQL.cls @@ -547,6 +547,7 @@ public virtual inherited sharing class SOQL implements Queryable { public Set 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(with(fieldToExtract, 'Id').groupBy(fieldToExtract).toAggregated()).keySet(); } diff --git a/force-app/main/default/classes/SOQL_Test.cls b/force-app/main/default/classes/SOQL_Test.cls index 4f25d74..eff7023 100644 --- a/force-app/main/default/classes/SOQL_Test.cls +++ b/force-app/main/default/classes/SOQL_Test.cls @@ -1348,6 +1348,19 @@ private class SOQL_Test { ); } + @IsTest + static void groupByWithDefaultFields() { + // Test + String soql = SOQL.of(Lead.SObjectType) + .with(Lead.FirstName, Lead.LastName, Lead.Email) + .with(Lead.LeadSource) + .groupBy(Lead.LeadSource) + .toString(); + + // Verify + Assert.areEqual('SELECT LeadSource FROM Lead GROUP BY LeadSource', soql); + } + @IsTest static void orderByString() { // Test