Skip to content

Commit f3942bf

Browse files
committed
Fixes a bug where calling addChildFields more than once with the same arguments lead to a 'Cannot follow the same aggregate relationship twice' error
1 parent 29cf5cb commit f3942bf

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

force-app/repository/Repository.cls

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
@SuppressWarnings('PMD.AvoidBooleanMethodParameters,PMD.ExcessiveParameterList,PMD.ExcessivePublicCount')
22
public virtual without sharing class Repository implements IRepository {
33
private final Map<Schema.SObjectField, String> childToRelationshipNames;
4+
private final Map<String, String> relationshipNameToChildQuery = new Map<String, String>();
45
private final IDML dml;
56
@TestVisible
67
private final List<Schema.SObjectField> queryFields;
@@ -174,6 +175,7 @@ public virtual without sharing class Repository implements IRepository {
174175
) {
175176
Repository cr = (Repository) childRepo;
176177
cr.selectFields.addAll(cr.addSelectFields());
178+
cr.selectFields.addAll(cr.relationshipNameToChildQuery.values());
177179
Set<String> localSelectFields = cr.selectFields;
178180
localSelectFields.remove('Id');
179181

@@ -213,12 +215,12 @@ public virtual without sharing class Repository implements IRepository {
213215
this.childBindVarKeys.addAll(query.getBindVars().keySet());
214216
}
215217

216-
this.selectFields.add(
217-
String.format(
218-
baseSubselect,
219-
new List<String>{ String.join(childFieldNames, ','), this.childToRelationshipNames.get(childFieldToken) }
220-
)
218+
String relationshipName = this.childToRelationshipNames.get(childFieldToken);
219+
String childFieldsToAdd = String.format(
220+
baseSubselect,
221+
new List<String>{ String.join(childFieldNames, ','), this.childToRelationshipNames.get(childFieldToken) }
221222
);
223+
this.relationshipNameToChildQuery.put(relationshipName, childFieldsToAdd);
222224
return this;
223225
}
224226

@@ -301,6 +303,7 @@ public virtual without sharing class Repository implements IRepository {
301303
localSelectFields.addAll(this.selectFields);
302304
this.baseSelectUsed = false;
303305
}
306+
localSelectFields.addAll(this.relationshipNameToChildQuery.values());
304307
return 'SELECT ' + String.join(localSelectFields, ', ') + '\nFROM ' + this.repoType;
305308
}
306309

force-app/repository/RepositoryTest.cls

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,52 @@ private class RepositoryTest {
144144
Assert.areEqual(acc.Name, returnedCon.Account.Name);
145145
}
146146

147+
@IsTest
148+
static void itSupportsCallingAddChildFieldsWithSameArgumentsTwice() {
149+
IRepository repo = new AccountRepo()
150+
.addChildFields(
151+
Contact.AccountId,
152+
new List<QueryField>{
153+
new QueryField(Contact.AccountId),
154+
new QueryField(Contact.LastName),
155+
new QueryField(
156+
new List<Schema.SObjectField>{ Contact.AccountId },
157+
new List<Schema.SObjectField>{ Account.Name }
158+
)
159+
},
160+
new List<Query>{ Query.notEquals(Account.Name, 'somepredicate') },
161+
new Map<String, RepositorySortOrder>(),
162+
1
163+
);
164+
repo.addChildFields(
165+
Contact.AccountId,
166+
new List<QueryField>{
167+
new QueryField(Contact.AccountId),
168+
new QueryField(Contact.LastName),
169+
new QueryField(
170+
new List<Schema.SObjectField>{ Contact.AccountId },
171+
new List<Schema.SObjectField>{ Account.Name }
172+
)
173+
},
174+
new List<Query>{ Query.notEquals(Account.Name, 'somepredicate') },
175+
new Map<String, RepositorySortOrder>(),
176+
1
177+
);
178+
179+
Account acc = new Account(Name = 'Parent');
180+
insert acc;
181+
Contact con = new Contact(AccountId = acc.Id, LastName = 'Child');
182+
insert con;
183+
184+
List<Account> accounts = repo.getAll();
185+
186+
Assert.areEqual(1, accounts.size());
187+
Assert.areEqual(1, accounts.get(0).Contacts.size());
188+
Contact returnedCon = accounts.get(0).Contacts.get(0);
189+
Assert.areEqual(con.LastName, returnedCon.LastName);
190+
Assert.areEqual(acc.Name, returnedCon.Account.Name);
191+
}
192+
147193
@IsTest
148194
static void itShouldAddChildFieldsFromRepo() {
149195
IRepository repo = new AccountRepo()

0 commit comments

Comments
 (0)