Skip to content

Commit 06f158b

Browse files
authored
SubQueries Binding Fix (#120)
Signed-off-by: Piotr PG Gajek <[email protected]>
1 parent f749175 commit 06f158b

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ public virtual inherited sharing class SOQL implements Queryable {
277277
// Config
278278

279279
private static Mock mock = new Mock();
280-
private static Binder binder;
280+
private static Binder binder = new Binder();
281281

282282
private QueryBuilder builder;
283283
private Executor executor;
@@ -707,7 +707,7 @@ public virtual inherited sharing class SOQL implements Queryable {
707707
}
708708

709709
public override String toString() {
710-
return builder.toString();
710+
return builder.resetBinding().toString();
711711
}
712712

713713
public Object toValueOf(SObjectField fieldToExtract) {
@@ -877,9 +877,12 @@ public virtual inherited sharing class SOQL implements Queryable {
877877
}
878878
}
879879

880-
public override String toString() {
880+
public QueryBuilder resetBinding() {
881881
binder = new Binder();
882+
return this;
883+
}
882884

885+
public override String toString() {
883886
String query = '';
884887

885888
for (QueryClause clause : clauses) {

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,49 @@ private class SOQL_Test {
855855
Assert.areEqual('SELECT Name , (SELECT Id, Name FROM Contacts FOR VIEW) FROM Account', soql);
856856
}
857857

858+
@IsTest
859+
static void multipleSubQueriesWithConditions() {
860+
// Setup
861+
String leadSource = 'Web';
862+
Date fromDate = Date.newInstance(2024, 1, 1);
863+
Date toDate = Date.newInstance(2024, 1, 30);
864+
865+
// Test
866+
SOQL builder = SOQL.of(Account.SObjectType)
867+
.with(
868+
SOQL.SubQuery.of('Contacts')
869+
.with(Contact.Id)
870+
.whereAre(SOQL.FilterGroup
871+
.add(SOQL.Filter.with(Contact.CreatedDate).lessOrEqual(fromDate))
872+
.add(SOQL.Filter.with(Contact.CreatedDate).isNull())
873+
.add(SOQL.Filter.with(Contact.CreatedDate).greaterOrEqual(toDate))
874+
.conditionLogic('1 AND (2 OR 3)')
875+
)
876+
)
877+
.with(
878+
SOQL.SubQuery.of('Opportunities')
879+
.with(Opportunity.Id)
880+
.whereAre(SOQL.FilterGroup
881+
.add(SOQL.Filter.with(Opportunity.LeadSource).equal(leadSource))
882+
.add(SOQL.Filter.with(Contact.CreatedDate).equal(fromDate))
883+
)
884+
)
885+
.setLimit(1);
886+
887+
// Verify
888+
Assert.areEqual(
889+
'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',
890+
builder.toString()
891+
);
892+
893+
Map<String, Object> binding = builder.binding();
894+
Assert.areEqual(fromDate, binding.get('v1'));
895+
Assert.areEqual(null, binding.get('v2'));
896+
Assert.areEqual(toDate, binding.get('v3'));
897+
Assert.areEqual(leadSource, binding.get('v4'));
898+
Assert.areEqual(fromDate, binding.get('v5'));
899+
}
900+
858901
@IsTest
859902
static void delegatedScope() {
860903
// Test

0 commit comments

Comments
 (0)