diff --git a/force-app/main/default/classes/SOQL.cls b/force-app/main/default/classes/SOQL.cls index fda9766..5dc1a58 100644 --- a/force-app/main/default/classes/SOQL.cls +++ b/force-app/main/default/classes/SOQL.cls @@ -51,6 +51,7 @@ public virtual inherited sharing class SOQL implements Queryable { Queryable with(SObjectField field1, SObjectField field2, SObjectField field3, SObjectField field4); Queryable with(SObjectField field1, SObjectField field2, SObjectField field3, SObjectField field4, SObjectField field5); Queryable with(List fields); + Queryable with(Iterable fields); Queryable with(String fields); Queryable with(SObjectField field, String alias); Queryable with(String relationshipName, SObjectField field); @@ -286,6 +287,10 @@ public virtual inherited sharing class SOQL implements Queryable { return this; } + public SOQL with(Iterable fields) { + return with(String.join(fields, ',')); + } + public SOQL with(String fields) { builder.fields.with(fields); return this; @@ -525,6 +530,9 @@ public virtual inherited sharing class SOQL implements Queryable { } public Integer toInteger() { + if (builder.fields.areCountsEmpty()) { + count(); + } return executor.toInteger(builder.toString(), binder.getBindingMap()); } @@ -732,6 +740,10 @@ public virtual inherited sharing class SOQL implements Queryable { fields.clear(); } + public Boolean areCountsEmpty() { + return counts.isEmpty(); + } + public override String toString() { if (fields.isEmpty() && counts.isEmpty()) { return 'SELECT Id'; diff --git a/force-app/main/default/classes/SOQL_Test.cls b/force-app/main/default/classes/SOQL_Test.cls index 2c3d552..40d634b 100644 --- a/force-app/main/default/classes/SOQL_Test.cls +++ b/force-app/main/default/classes/SOQL_Test.cls @@ -135,6 +135,23 @@ private class SOQL_Test { Assert.areEqual('SELECT Id, Name, Industry, AccountNumber, AnnualRevenue, BillingCity FROM Account', soql); } + @IsTest + static void withListOfStringFields() { + // Test + String soql = SOQL.of(Account.SObjectType) + .with(new List{ + 'Id', + 'Name', + 'Industry', + 'AccountNumber', + 'AnnualRevenue', + 'BillingCity' + }).toString(); + + // Verify + Assert.areEqual('SELECT Id, Name, Industry, AccountNumber, AnnualRevenue, BillingCity FROM Account', soql); + } + @IsTest static void withStringFields() { // Test @@ -1784,6 +1801,18 @@ private class SOQL_Test { Assert.areEqual(accounts.size(), result[0].get('names')); } + @IsTest + static void toIntegerWithoutCount() { + // Setup + List accounts = insertAccounts(); + + // Test + Integer result = SOQL.of(Account.SObjectType).toInteger(); + + // Verify + Assert.areEqual(accounts.size(), result); + } + @IsTest static void toInteger() { // Setup diff --git a/website/docs/api/soql.md b/website/docs/api/soql.md index 5a0b07e..78cbb19 100644 --- a/website/docs/api/soql.md +++ b/website/docs/api/soql.md @@ -23,6 +23,7 @@ The following are methods for `SOQL`. - [`with(SObjectField field1, SObjectField field2, SObjectField field3, SObjectField field4)`](#with-field1---field5) - [`with(SObjectField field1, SObjectField field2, SObjectField field3, SObjectField field4, SObjectField field5)`](#with-field1---field5) - [`with(List fields)`](#with-fields) +- [`with(List fields)`](#with-fields) - [`with(String fields)`](#with-string-fields) - [`with(String relationshipName, SObjectField field)`](#with-related-field1---field5) - [`with(String relationshipName, SObjectField field1, SObjectField field2)`](#with-related-field1---field5) @@ -54,8 +55,8 @@ The following are methods for `SOQL`. - [`whereAre(FilterGroup filterGroup)`](#whereare) - [`whereAre(Filter filter)`](#whereare) -- [conditionLogic(String order)](#conditionlogic) -- [anyConditionMatching()](#anyconditionmatching); +- [`conditionLogic(String order)`](#conditionlogic) +- [`anyConditionMatching()`](#anyconditionmatching); [**GROUP BY**](#group-by) @@ -199,6 +200,7 @@ Use for more than 5 fields. ```apex SOQL with(List fields) +SOQL with(List fields) ``` **Example** @@ -217,6 +219,16 @@ SOQL.of(Account.SObjectType) Account.AnnualRevenue, Account.BillingCity }).toList(); + +SOQL.of(Account.SObjectType) + .with(new List{ + 'Id', + 'Name', + 'Industry', + 'AccountNumber', + 'AnnualRevenue', + 'BillingCity' + }).toList(); ``` ### with string fields