diff --git a/example-app/ExampleFactory.cls-meta.xml b/example-app/ExampleFactory.cls-meta.xml index 3a10d2e..ba7ea1b 100644 --- a/example-app/ExampleFactory.cls-meta.xml +++ b/example-app/ExampleFactory.cls-meta.xml @@ -1,5 +1,5 @@ - 60.0 + 62.0 Active \ No newline at end of file diff --git a/example-app/ExampleRepoFactory.cls-meta.xml b/example-app/ExampleRepoFactory.cls-meta.xml index 3a10d2e..ba7ea1b 100644 --- a/example-app/ExampleRepoFactory.cls-meta.xml +++ b/example-app/ExampleRepoFactory.cls-meta.xml @@ -1,5 +1,5 @@ - 60.0 + 62.0 Active \ No newline at end of file diff --git a/example-app/handlers/AccountHandler.cls-meta.xml b/example-app/handlers/AccountHandler.cls-meta.xml index 3a10d2e..ba7ea1b 100644 --- a/example-app/handlers/AccountHandler.cls-meta.xml +++ b/example-app/handlers/AccountHandler.cls-meta.xml @@ -1,5 +1,5 @@ - 60.0 + 62.0 Active \ No newline at end of file diff --git a/example-app/handlers/AccountHandlerTests.cls-meta.xml b/example-app/handlers/AccountHandlerTests.cls-meta.xml index 3a10d2e..ba7ea1b 100644 --- a/example-app/handlers/AccountHandlerTests.cls-meta.xml +++ b/example-app/handlers/AccountHandlerTests.cls-meta.xml @@ -1,5 +1,5 @@ - 60.0 + 62.0 Active \ No newline at end of file diff --git a/example-app/handlers/ExampleSObjectTest.cls-meta.xml b/example-app/handlers/ExampleSObjectTest.cls-meta.xml index 3a10d2e..ba7ea1b 100644 --- a/example-app/handlers/ExampleSObjectTest.cls-meta.xml +++ b/example-app/handlers/ExampleSObjectTest.cls-meta.xml @@ -1,5 +1,5 @@ - 60.0 + 62.0 Active \ No newline at end of file diff --git a/example-app/triggers/TriggerHandler.cls-meta.xml b/example-app/triggers/TriggerHandler.cls-meta.xml index 3a10d2e..ba7ea1b 100644 --- a/example-app/triggers/TriggerHandler.cls-meta.xml +++ b/example-app/triggers/TriggerHandler.cls-meta.xml @@ -1,5 +1,5 @@ - 60.0 + 62.0 Active \ No newline at end of file diff --git a/example-app/triggers/TriggerHandler_Tests.cls-meta.xml b/example-app/triggers/TriggerHandler_Tests.cls-meta.xml index 3a10d2e..ba7ea1b 100644 --- a/example-app/triggers/TriggerHandler_Tests.cls-meta.xml +++ b/example-app/triggers/TriggerHandler_Tests.cls-meta.xml @@ -1,5 +1,5 @@ - 60.0 + 62.0 Active \ No newline at end of file diff --git a/force-app/dml/DML.cls-meta.xml b/force-app/dml/DML.cls-meta.xml index 3a10d2e..ba7ea1b 100644 --- a/force-app/dml/DML.cls-meta.xml +++ b/force-app/dml/DML.cls-meta.xml @@ -1,5 +1,5 @@ - 60.0 + 62.0 Active \ No newline at end of file diff --git a/force-app/dml/DMLMock.cls-meta.xml b/force-app/dml/DMLMock.cls-meta.xml index 3a10d2e..ba7ea1b 100644 --- a/force-app/dml/DMLMock.cls-meta.xml +++ b/force-app/dml/DMLMock.cls-meta.xml @@ -1,5 +1,5 @@ - 60.0 + 62.0 Active \ No newline at end of file diff --git a/force-app/dml/DMLTests.cls-meta.xml b/force-app/dml/DMLTests.cls-meta.xml index 3a10d2e..ba7ea1b 100644 --- a/force-app/dml/DMLTests.cls-meta.xml +++ b/force-app/dml/DMLTests.cls-meta.xml @@ -1,5 +1,5 @@ - 60.0 + 62.0 Active \ No newline at end of file diff --git a/force-app/dml/IDML.cls-meta.xml b/force-app/dml/IDML.cls-meta.xml index 3a10d2e..ba7ea1b 100644 --- a/force-app/dml/IDML.cls-meta.xml +++ b/force-app/dml/IDML.cls-meta.xml @@ -1,5 +1,5 @@ - 60.0 + 62.0 Active \ No newline at end of file diff --git a/force-app/factory/Factory.cls-meta.xml b/force-app/factory/Factory.cls-meta.xml index 3a10d2e..ba7ea1b 100644 --- a/force-app/factory/Factory.cls-meta.xml +++ b/force-app/factory/Factory.cls-meta.xml @@ -1,5 +1,5 @@ - 60.0 + 62.0 Active \ No newline at end of file diff --git a/force-app/factory/RepoFactory.cls-meta.xml b/force-app/factory/RepoFactory.cls-meta.xml index 3a10d2e..ba7ea1b 100644 --- a/force-app/factory/RepoFactory.cls-meta.xml +++ b/force-app/factory/RepoFactory.cls-meta.xml @@ -1,5 +1,5 @@ - 60.0 + 62.0 Active \ No newline at end of file diff --git a/force-app/factory/RepoFactoryMock.cls-meta.xml b/force-app/factory/RepoFactoryMock.cls-meta.xml index 3a10d2e..ba7ea1b 100644 --- a/force-app/factory/RepoFactoryMock.cls-meta.xml +++ b/force-app/factory/RepoFactoryMock.cls-meta.xml @@ -1,5 +1,5 @@ - 60.0 + 62.0 Active \ No newline at end of file diff --git a/force-app/repository/AdditionalSoslObject.cls-meta.xml b/force-app/repository/AdditionalSoslObject.cls-meta.xml index 3a10d2e..ba7ea1b 100644 --- a/force-app/repository/AdditionalSoslObject.cls-meta.xml +++ b/force-app/repository/AdditionalSoslObject.cls-meta.xml @@ -1,5 +1,5 @@ - 60.0 + 62.0 Active \ No newline at end of file diff --git a/force-app/repository/AggregateRecord.cls-meta.xml b/force-app/repository/AggregateRecord.cls-meta.xml index f5e18fd..998805a 100644 --- a/force-app/repository/AggregateRecord.cls-meta.xml +++ b/force-app/repository/AggregateRecord.cls-meta.xml @@ -1,5 +1,5 @@ - 60.0 + 62.0 Active diff --git a/force-app/repository/AggregateRepository.cls-meta.xml b/force-app/repository/AggregateRepository.cls-meta.xml index 3a10d2e..ba7ea1b 100644 --- a/force-app/repository/AggregateRepository.cls-meta.xml +++ b/force-app/repository/AggregateRepository.cls-meta.xml @@ -1,5 +1,5 @@ - 60.0 + 62.0 Active \ No newline at end of file diff --git a/force-app/repository/AggregateRepositoryTests.cls-meta.xml b/force-app/repository/AggregateRepositoryTests.cls-meta.xml index 3a10d2e..ba7ea1b 100644 --- a/force-app/repository/AggregateRepositoryTests.cls-meta.xml +++ b/force-app/repository/AggregateRepositoryTests.cls-meta.xml @@ -1,5 +1,5 @@ - 60.0 + 62.0 Active \ No newline at end of file diff --git a/force-app/repository/Aggregation.cls-meta.xml b/force-app/repository/Aggregation.cls-meta.xml index 3a10d2e..ba7ea1b 100644 --- a/force-app/repository/Aggregation.cls-meta.xml +++ b/force-app/repository/Aggregation.cls-meta.xml @@ -1,5 +1,5 @@ - 60.0 + 62.0 Active \ No newline at end of file diff --git a/force-app/repository/AggregationTests.cls-meta.xml b/force-app/repository/AggregationTests.cls-meta.xml index 3a10d2e..ba7ea1b 100644 --- a/force-app/repository/AggregationTests.cls-meta.xml +++ b/force-app/repository/AggregationTests.cls-meta.xml @@ -1,5 +1,5 @@ - 60.0 + 62.0 Active \ No newline at end of file diff --git a/force-app/repository/DateFunction.cls-meta.xml b/force-app/repository/DateFunction.cls-meta.xml index 3a10d2e..ba7ea1b 100644 --- a/force-app/repository/DateFunction.cls-meta.xml +++ b/force-app/repository/DateFunction.cls-meta.xml @@ -1,5 +1,5 @@ - 60.0 + 62.0 Active \ No newline at end of file diff --git a/force-app/repository/FieldLevelHistory.cls-meta.xml b/force-app/repository/FieldLevelHistory.cls-meta.xml index 3a10d2e..ba7ea1b 100644 --- a/force-app/repository/FieldLevelHistory.cls-meta.xml +++ b/force-app/repository/FieldLevelHistory.cls-meta.xml @@ -1,5 +1,5 @@ - 60.0 + 62.0 Active \ No newline at end of file diff --git a/force-app/repository/FieldLevelHistoryRepo.cls-meta.xml b/force-app/repository/FieldLevelHistoryRepo.cls-meta.xml index 3a10d2e..ba7ea1b 100644 --- a/force-app/repository/FieldLevelHistoryRepo.cls-meta.xml +++ b/force-app/repository/FieldLevelHistoryRepo.cls-meta.xml @@ -1,5 +1,5 @@ - 60.0 + 62.0 Active \ No newline at end of file diff --git a/force-app/repository/FieldLevelHistoryRepoTests.cls-meta.xml b/force-app/repository/FieldLevelHistoryRepoTests.cls-meta.xml index 3a10d2e..ba7ea1b 100644 --- a/force-app/repository/FieldLevelHistoryRepoTests.cls-meta.xml +++ b/force-app/repository/FieldLevelHistoryRepoTests.cls-meta.xml @@ -1,5 +1,5 @@ - 60.0 + 62.0 Active \ No newline at end of file diff --git a/force-app/repository/IAggregateRepository.cls-meta.xml b/force-app/repository/IAggregateRepository.cls-meta.xml index 3a10d2e..ba7ea1b 100644 --- a/force-app/repository/IAggregateRepository.cls-meta.xml +++ b/force-app/repository/IAggregateRepository.cls-meta.xml @@ -1,5 +1,5 @@ - 60.0 + 62.0 Active \ No newline at end of file diff --git a/force-app/repository/IHistoryRepository.cls-meta.xml b/force-app/repository/IHistoryRepository.cls-meta.xml index 3a10d2e..ba7ea1b 100644 --- a/force-app/repository/IHistoryRepository.cls-meta.xml +++ b/force-app/repository/IHistoryRepository.cls-meta.xml @@ -1,5 +1,5 @@ - 60.0 + 62.0 Active \ No newline at end of file diff --git a/force-app/repository/IRepository.cls b/force-app/repository/IRepository.cls index 13bd57d..e312f5c 100644 --- a/force-app/repository/IRepository.cls +++ b/force-app/repository/IRepository.cls @@ -26,4 +26,11 @@ public interface IRepository extends IDML { Map fieldToSortOrder, Integer limitBy ); + IRepository addChildFields( + Schema.SObjectField childFieldToken, + List childFields, + List optionalWhereFilters, + Map fieldToSortOrder, + Integer limitBy + ); } diff --git a/force-app/repository/IRepository.cls-meta.xml b/force-app/repository/IRepository.cls-meta.xml index 3a10d2e..ba7ea1b 100644 --- a/force-app/repository/IRepository.cls-meta.xml +++ b/force-app/repository/IRepository.cls-meta.xml @@ -1,5 +1,5 @@ - 60.0 + 62.0 Active \ No newline at end of file diff --git a/force-app/repository/Query.cls-meta.xml b/force-app/repository/Query.cls-meta.xml index 3a10d2e..ba7ea1b 100644 --- a/force-app/repository/Query.cls-meta.xml +++ b/force-app/repository/Query.cls-meta.xml @@ -1,5 +1,5 @@ - 60.0 + 62.0 Active \ No newline at end of file diff --git a/force-app/repository/QueryField.cls b/force-app/repository/QueryField.cls new file mode 100644 index 0000000..602a25b --- /dev/null +++ b/force-app/repository/QueryField.cls @@ -0,0 +1,39 @@ +public without sharing virtual class QueryField { + private final String fieldValue; + + public QueryField(List fieldTokens) { + this(getFieldNames(fieldTokens)); + } + + public QueryField(List fieldNames) { + this.fieldValue = String.join(fieldNames, ','); + } + + public QueryField(Schema.SObjectField token) { + this.fieldValue = token.toString(); + } + + public QueryField(List parentFieldChain, List parentFields) { + String base = ''; + while (parentFieldChain.isEmpty() == false) { + base = parentFieldChain.remove(0).getDescribe().getRelationshipName() + '.'; + } + List fields = new List(); + for (Schema.SObjectField field : parentFields) { + fields.add(base + field.toString()); + } + this.fieldValue = String.join(fields, ','); + } + + public override String toString() { + return this.fieldValue; + } + + private static List getFieldNames(List fieldTokens) { + List fieldNames = new List(); + for (Schema.SObjectField token : fieldTokens) { + fieldNames.add(token.toString()); + } + return fieldNames; + } +} diff --git a/force-app/repository/QueryField.cls-meta.xml b/force-app/repository/QueryField.cls-meta.xml new file mode 100644 index 0000000..998805a --- /dev/null +++ b/force-app/repository/QueryField.cls-meta.xml @@ -0,0 +1,5 @@ + + + 62.0 + Active + diff --git a/force-app/repository/QueryTests.cls-meta.xml b/force-app/repository/QueryTests.cls-meta.xml index 3a10d2e..ba7ea1b 100644 --- a/force-app/repository/QueryTests.cls-meta.xml +++ b/force-app/repository/QueryTests.cls-meta.xml @@ -1,5 +1,5 @@ - 60.0 + 62.0 Active \ No newline at end of file diff --git a/force-app/repository/Repository.cls b/force-app/repository/Repository.cls index 5b73f93..3ac8afb 100644 --- a/force-app/repository/Repository.cls +++ b/force-app/repository/Repository.cls @@ -1,6 +1,7 @@ public virtual without sharing class Repository implements IRepository { private final Map childToRelationshipNames; private final IDML dml; + @TestVisible private final List queryFields; private final Set selectFields = new Set(); @@ -79,13 +80,7 @@ public virtual without sharing class Repository implements IRepository { } public Repository addParentFields(List parentTypes, List parentFields) { - String parentBase = ''; - for (SObjectField parentId : parentTypes) { - parentBase += parentId.getDescribe().getRelationshipName() + '.'; - } - for (SObjectField parentField : parentFields) { - this.selectFields.add(parentBase + parentField.getDescribe().getName()); - } + this.selectFields.addAll(this.getParentFields(parentTypes, parentFields)); return this; } @@ -105,6 +100,22 @@ public virtual without sharing class Repository implements IRepository { List optionalWhereFilters, Map fieldToSortOrder, Integer limitAmount + ) { + return this.addChildFields( + childFieldToken, + new List{ new QueryField(childFields) }, + optionalWhereFilters, + fieldToSortOrder, + limitAmount + ); + } + + public Repository addChildFields( + Schema.SObjectField childFieldToken, + List childFields, + List optionalWhereFilters, + Map fieldToSortOrder, + Integer limitBy ) { if (this.childToRelationshipNames.containsKey(childFieldToken) == false || this.shouldAddChildFields == false) { return this; @@ -118,8 +129,8 @@ public virtual without sharing class Repository implements IRepository { ')'; Set childFieldNames = new Set{ 'Id' }; - for (SObjectField childField : childFields) { - childFieldNames.add(childField.getDescribe().getName()); + for (QueryField childField : childFields) { + childFieldNames.add(childField.toString()); } this.selectFields.add( @@ -158,6 +169,22 @@ public virtual without sharing class Repository implements IRepository { this.limitAmount = null; } + private List getParentFields( + List parentTypes, + List parentFieldTokens + ) { + List parentFields = new List(); + String parentBase = ''; + for (Schema.SObjectField parentToken : parentTypes) { + String parentName = parentToken.getDescribe().getRelationshipName() ?? parentToken.toString().replace('__c', '__r'); + parentBase += parentName + '.'; + } + for (Schema.SObjectField parentField : parentFieldTokens) { + parentFields.add(parentBase + parentField.getDescribe().getName()); + } + return parentFields; + } + private Map getChildRelationshipNames(Schema.SObjectType repoType) { Map localChildToRelationshipNames = new Map(); for (Schema.ChildRelationship childRelationship : repoType.getDescribe().getChildRelationships()) { @@ -276,6 +303,7 @@ public virtual without sharing class Repository implements IRepository { return String.join(objectsPreJoin, ','); } + // DML public Database.SaveResult doInsert(SObject record) { return this.dml.doInsert(record); } diff --git a/force-app/repository/Repository.cls-meta.xml b/force-app/repository/Repository.cls-meta.xml index 3a10d2e..ba7ea1b 100644 --- a/force-app/repository/Repository.cls-meta.xml +++ b/force-app/repository/Repository.cls-meta.xml @@ -1,5 +1,5 @@ - 60.0 + 62.0 Active \ No newline at end of file diff --git a/force-app/repository/RepositorySortOrder.cls-meta.xml b/force-app/repository/RepositorySortOrder.cls-meta.xml index 3a10d2e..ba7ea1b 100644 --- a/force-app/repository/RepositorySortOrder.cls-meta.xml +++ b/force-app/repository/RepositorySortOrder.cls-meta.xml @@ -1,5 +1,5 @@ - 60.0 + 62.0 Active \ No newline at end of file diff --git a/force-app/repository/RepositoryTests.cls b/force-app/repository/RepositoryTests.cls index 3d89717..9873e88 100644 --- a/force-app/repository/RepositoryTests.cls +++ b/force-app/repository/RepositoryTests.cls @@ -37,8 +37,8 @@ private class RepositoryTests { IRepository repo = new ContactPointAddressRepo(); - List opps = repo.get(listQuery); - System.assertEquals(0, opps.size()); + List results = repo.get(listQuery); + System.assertEquals(0, results.size()); System.assertEquals(1, Limits.getQueries()); } @@ -99,7 +99,20 @@ private class RepositoryTests { @IsTest static void it_should_add_child_fields() { IRepository repo = new AccountRepo() - .addChildFields(Contact.AccountId, new List{ Contact.AccountId, Contact.LastName }); + .addChildFields( + Contact.AccountId, + new List{ + new QueryField(Contact.AccountId), + new QueryField(Contact.LastName), + new QueryField( + new List{ Contact.AccountId }, + new List{ Account.Name } + ) + }, + new List(), + new Map(), + 1 + ); Account acc = new Account(Name = 'Parent'); insert acc; @@ -111,7 +124,8 @@ private class RepositoryTests { System.assertEquals(1, accounts.size()); System.assertEquals(1, accounts.get(0).Contacts.size()); Contact returnedCon = accounts.get(0).Contacts.get(0); - System.assertEquals(con, returnedCon); + System.assertEquals(con.LastName, returnedCon.LastName); + System.assertEquals(acc.Name, returnedCon.Account.Name); } @IsTest @@ -171,8 +185,7 @@ private class RepositoryTests { accounts[2].ParentId = accounts[3].Id; update accounts; - IRepository accountRepo = new AccountRepo(); - List returnedAccounts = accountRepo + List returnedAccounts = new AccountRepo() .addParentFields( new List{ Account.ParentId }, new List{ Account.Id, Account.Name } diff --git a/force-app/repository/RepositoryTests.cls-meta.xml b/force-app/repository/RepositoryTests.cls-meta.xml index 3a10d2e..ba7ea1b 100644 --- a/force-app/repository/RepositoryTests.cls-meta.xml +++ b/force-app/repository/RepositoryTests.cls-meta.xml @@ -1,5 +1,5 @@ - 60.0 + 62.0 Active \ No newline at end of file diff --git a/force-app/repository/SearchGroup.cls-meta.xml b/force-app/repository/SearchGroup.cls-meta.xml index 3a10d2e..ba7ea1b 100644 --- a/force-app/repository/SearchGroup.cls-meta.xml +++ b/force-app/repository/SearchGroup.cls-meta.xml @@ -1,5 +1,5 @@ - 60.0 + 62.0 Active \ No newline at end of file diff --git a/force-app/utils/TestingUtils.cls-meta.xml b/force-app/utils/TestingUtils.cls-meta.xml index 3a10d2e..ba7ea1b 100644 --- a/force-app/utils/TestingUtils.cls-meta.xml +++ b/force-app/utils/TestingUtils.cls-meta.xml @@ -1,5 +1,5 @@ - 60.0 + 62.0 Active \ No newline at end of file diff --git a/sfdx-project.json b/sfdx-project.json index 3586b18..26cccbc 100644 --- a/sfdx-project.json +++ b/sfdx-project.json @@ -10,5 +10,5 @@ ], "namespace": "", "sfdcLoginUrl": "https://login.salesforce.com", - "sourceApiVersion": "60.0" + "sourceApiVersion": "62.0" } \ No newline at end of file