From 06f158bcd775b98afce5112a4416df9ca7a9538c Mon Sep 17 00:00:00 2001 From: Piotr Gajek Date: Wed, 8 May 2024 14:36:37 +0200 Subject: [PATCH] SubQueries Binding Fix (#120) Signed-off-by: Piotr PG Gajek --- force-app/main/default/classes/SOQL.cls | 9 ++-- force-app/main/default/classes/SOQL_Test.cls | 43 ++++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/force-app/main/default/classes/SOQL.cls b/force-app/main/default/classes/SOQL.cls index b053da1..babe597 100644 --- a/force-app/main/default/classes/SOQL.cls +++ b/force-app/main/default/classes/SOQL.cls @@ -277,7 +277,7 @@ public virtual inherited sharing class SOQL implements Queryable { // Config private static Mock mock = new Mock(); - private static Binder binder; + private static Binder binder = new Binder(); private QueryBuilder builder; private Executor executor; @@ -707,7 +707,7 @@ public virtual inherited sharing class SOQL implements Queryable { } public override String toString() { - return builder.toString(); + return builder.resetBinding().toString(); } public Object toValueOf(SObjectField fieldToExtract) { @@ -877,9 +877,12 @@ public virtual inherited sharing class SOQL implements Queryable { } } - public override String toString() { + public QueryBuilder resetBinding() { binder = new Binder(); + return this; + } + public override String toString() { String query = ''; for (QueryClause clause : clauses) { diff --git a/force-app/main/default/classes/SOQL_Test.cls b/force-app/main/default/classes/SOQL_Test.cls index e8e1b29..c0af147 100644 --- a/force-app/main/default/classes/SOQL_Test.cls +++ b/force-app/main/default/classes/SOQL_Test.cls @@ -855,6 +855,49 @@ private class SOQL_Test { Assert.areEqual('SELECT Name , (SELECT Id, Name FROM Contacts FOR VIEW) FROM Account', soql); } + @IsTest + static void multipleSubQueriesWithConditions() { + // Setup + String leadSource = 'Web'; + Date fromDate = Date.newInstance(2024, 1, 1); + Date toDate = Date.newInstance(2024, 1, 30); + + // Test + SOQL builder = SOQL.of(Account.SObjectType) + .with( + SOQL.SubQuery.of('Contacts') + .with(Contact.Id) + .whereAre(SOQL.FilterGroup + .add(SOQL.Filter.with(Contact.CreatedDate).lessOrEqual(fromDate)) + .add(SOQL.Filter.with(Contact.CreatedDate).isNull()) + .add(SOQL.Filter.with(Contact.CreatedDate).greaterOrEqual(toDate)) + .conditionLogic('1 AND (2 OR 3)') + ) + ) + .with( + SOQL.SubQuery.of('Opportunities') + .with(Opportunity.Id) + .whereAre(SOQL.FilterGroup + .add(SOQL.Filter.with(Opportunity.LeadSource).equal(leadSource)) + .add(SOQL.Filter.with(Contact.CreatedDate).equal(fromDate)) + ) + ) + .setLimit(1); + + // Verify + Assert.areEqual( + 'SELECT Id , (SELECT Id FROM Contacts WHERE (CreatedDate <= :v1 AND (CreatedDate = :v2 OR CreatedDate >= :v3))), (SELECT Id FROM Opportunities WHERE (LeadSource = :v4 AND CreatedDate = :v5)) FROM Account LIMIT 1', + builder.toString() + ); + + Map binding = builder.binding(); + Assert.areEqual(fromDate, binding.get('v1')); + Assert.areEqual(null, binding.get('v2')); + Assert.areEqual(toDate, binding.get('v3')); + Assert.areEqual(leadSource, binding.get('v4')); + Assert.areEqual(fromDate, binding.get('v5')); + } + @IsTest static void delegatedScope() { // Test