Add getChildIdsById to fflib_SObjects2

To retrieve a map of related ids from (grand-)child relationships

Signed-off-by: WimVelzeboer <william@velzeboer.nl>
This commit is contained in:
WimVelzeboer
2023-04-20 16:21:00 +01:00
parent 18009957bf
commit a4753d1db1
4 changed files with 143 additions and 1 deletions
@@ -120,6 +120,48 @@ private class fflib_SObjects2Test
// todo - add assertions
}
@IsTest
static void itShouldReturnTheRelatedChildDomainId()
{
// GIVEN a case A & Z related to contact A related to Account,
Id caseIdA = fflib_IDGenerator.generate(Schema.Case.SObjectType);
Id caseIdZ = fflib_IDGenerator.generate(Schema.Case.SObjectType);
Id caseIdB = fflib_IDGenerator.generate(Schema.Case.SObjectType);
Id caseIdC = fflib_IDGenerator.generate(Schema.Case.SObjectType);
Id contactIdA = fflib_IDGenerator.generate(Schema.Contact.SObjectType);
Id contactIdB = fflib_IDGenerator.generate(Schema.Contact.SObjectType);
Id accountId = fflib_IDGenerator.generate(Schema.Account.SObjectType);
Account accountRecord = new Account(Id = accountId);
Contact contactRecordA = new Contact(Id = contactIdA, AccountId = accountId);
Contact contactRecordB = new Contact(Id = contactIdB);
Case caseRecordA = new Case(Id = caseIdA, ContactId = contactIdA);
Case caseRecordZ = new Case(Id = caseIdZ, ContactId = contactIdA);
Case caseRecordB = new Case(Id = caseIdB, ContactId = contactIdB);
Case caseRecordC = new Case(Id = caseIdC);
DomainAccounts accountDomain = new DomainAccounts(new List<Account> {accountRecord});
DomainContacts contactDomain = new DomainContacts(new List<Contact> {contactRecordA, contactRecordB});
DomainCases caseDomain = new DomainCases(new List<Case> {caseRecordA, caseRecordB, caseRecordC, caseRecordZ});
// WHEN we request the relation between Case and Account
System.Test.startTest();
Map<Id, Set<Id>> result = accountDomain.getChildIdsById(
new List<fflib_SObjects2>
{
contactDomain,
caseDomain
}
);
System.Test.stopTest();
System.debug('result: ' + JSON.serializePretty(result));
// THEN it should return the AccountId By CaseId
System.Assert.areEqual(1, result.size(), 'Expected one account');
System.Assert.isTrue(result.containsKey(accountId), 'Account Id should be the main key');
System.Assert.areEqual(2, result.get(accountId).size(), 'Incorrect amount of related cases to the account');
System.Assert.isTrue(result.get(accountId).containsAll(new Set<Id>{ caseIdA, caseIdZ }));
}
@IsTest
static void itShouldReturnTheRelatedParentDomainId()
{
@@ -668,6 +710,14 @@ private class fflib_SObjects2Test
{
clearField(Schema.Account.ShippingCountry);
}
protected override Map<SObjectType, Schema.SObjectField> getRelatedFieldByDomainType()
{
return new Map<SObjectType, Schema.SObjectField>
{
Schema.Contact.SObjectType => Contact.AccountId
};
}
}
private class DomainContacts extends fflib_SObjects2
@@ -681,7 +731,8 @@ private class fflib_SObjects2Test
{
return new Map<SObjectType, Schema.SObjectField>
{
Schema.Account.SObjectType => Contact.AccountId
Schema.Account.SObjectType => Contact.AccountId,
Schema.Case.SObjectType => Case.ContactId
};
}
}