Skip to content

Commit 9890745

Browse files
committed
Add methods to retrieve specific records
1 parent 1e5eb56 commit 9890745

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

sfdx-source/apex-common/main/classes/fflib_SObjects.cls

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,61 @@ public virtual class fflib_SObjects
5454
SObjectDescribe = sObjectType.getDescribe();
5555
}
5656

57+
/**
58+
* @param id Domain containing primary key (Id field) values
59+
*
60+
* @return Returns only the SObjects from the domain matching the given Ids.
61+
*/
62+
public virtual SObject getRecord(Id id)
63+
{
64+
List<SObject> result = getRecords(new Set<Id> {id});
65+
66+
return (result.size() == 0) ? null : result.get(0);
67+
}
68+
69+
/**
70+
* @return Returns the contents of the Domain by their primary Key ('Id' field)
71+
*/
72+
public virtual Map<Id, SObject> getRecordsById()
73+
{
74+
return new Map<Id, SObject>(getRecords());
75+
}
76+
77+
/**
78+
* @param ids A Set containing primary key (Id field) values
79+
*
80+
* @return Returns only the SObjects from the domain matching the given Ids.
81+
*/
82+
public virtual List<SObject> getRecords(Set<Id> ids)
83+
{
84+
Map<Id, SObject> sObjectsByIds = getRecordsById();
85+
List<SObject> result = new List<SObject>();
86+
for (Id id : ids)
87+
{
88+
if (sObjectsByIds.containsKey(id) == false) continue;
89+
90+
result.add(sObjectsByIds.get(id));
91+
}
92+
return result;
93+
}
94+
95+
/**
96+
* @param ids A Set containing primary key (Id field) values
97+
*
98+
* @return Returns the SObjects from the domain which are not part of the given Ids.
99+
*/
100+
public virtual List<SObject> getRecordsNotIn(Set<Id> ids)
101+
{
102+
List<SObject> result = new List<SObject>();
103+
for (SObject record : getRecords())
104+
{
105+
if (ids.contains(record.Id)) continue;
106+
107+
result.add(record);
108+
}
109+
return result;
110+
}
111+
57112
public virtual List<SObject> getRecords()
58113
{
59114
return (List<SObject>) getObjects();

sfdx-source/apex-common/test/classes/fflib_SObjectsTest.cls

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,38 @@ private class fflib_SObjectsTest
4545
);
4646
}
4747

48+
@IsTest
49+
static void itShouldReturnRecordsByIds()
50+
{
51+
final Id accountIdA = fflib_IDGenerator.generate(Schema.Account.SObjectType);
52+
final Id accountIdB = fflib_IDGenerator.generate(Schema.Account.SObjectType);
53+
final Id accountIdC = fflib_IDGenerator.generate(Schema.Account.SObjectType);
54+
55+
fflib_SObjects domain = new fflib_SObjects(
56+
new List<SObject>
57+
{
58+
new Account(Id = accountIdA, Name = 'A'),
59+
new Account(Id = accountIdB, Name = 'B'),
60+
new Account(Id = accountIdC, Name = 'C')
61+
});
62+
63+
Set<Id> idsToRetrieve = new Set<Id> {accountIdA, accountIdC};
64+
65+
// Get just a subset of the domain
66+
List<SObject> subSetRecords = domain.getRecords(idsToRetrieve);
67+
Set<Id> resultIds = new Map<Id, SObject>(subSetRecords).keySet();
68+
System.assertEquals(2, subSetRecords.size(), 'Unexpected amount of record returned');
69+
System.assert(resultIds.containsAll(idsToRetrieve), 'Did not retrieve the correct records');
70+
71+
// Get all the other records
72+
List<SObject> allOtherRecords = domain.getRecordsNotIn(idsToRetrieve);
73+
System.assertEquals(1, allOtherRecords.size(), 'Unexpected amount of record returned');
74+
System.assertEquals(accountIdB, allOtherRecords.get(0).Id, 'Did not retrieve the correct record');
75+
76+
// Get a single specific record
77+
System.assertEquals(accountIdB, domain.getRecord(accountIdB).Id, 'Incorrect record retrieved from domain');
78+
}
79+
4880
@IsTest
4981
static void itShouldReturnRecordsWithFieldValues()
5082
{

0 commit comments

Comments
 (0)