Skip to content

Commit

Permalink
Add methods to retrieve specific records
Browse files Browse the repository at this point in the history
  • Loading branch information
wimvelzeboer committed Apr 6, 2022
1 parent 1e5eb56 commit 9890745
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
55 changes: 55 additions & 0 deletions sfdx-source/apex-common/main/classes/fflib_SObjects.cls
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,61 @@ public virtual class fflib_SObjects
SObjectDescribe = sObjectType.getDescribe();
}

/**
* @param id Domain containing primary key (Id field) values
*
* @return Returns only the SObjects from the domain matching the given Ids.
*/
public virtual SObject getRecord(Id id)
{
List<SObject> result = getRecords(new Set<Id> {id});

return (result.size() == 0) ? null : result.get(0);
}

/**
* @return Returns the contents of the Domain by their primary Key ('Id' field)
*/
public virtual Map<Id, SObject> getRecordsById()
{
return new Map<Id, SObject>(getRecords());
}

/**
* @param ids A Set containing primary key (Id field) values
*
* @return Returns only the SObjects from the domain matching the given Ids.
*/
public virtual List<SObject> getRecords(Set<Id> ids)
{
Map<Id, SObject> sObjectsByIds = getRecordsById();
List<SObject> result = new List<SObject>();
for (Id id : ids)
{
if (sObjectsByIds.containsKey(id) == false) continue;

result.add(sObjectsByIds.get(id));
}
return result;
}

/**
* @param ids A Set containing primary key (Id field) values
*
* @return Returns the SObjects from the domain which are not part of the given Ids.
*/
public virtual List<SObject> getRecordsNotIn(Set<Id> ids)
{
List<SObject> result = new List<SObject>();
for (SObject record : getRecords())
{
if (ids.contains(record.Id)) continue;

result.add(record);
}
return result;
}

public virtual List<SObject> getRecords()
{
return (List<SObject>) getObjects();
Expand Down
32 changes: 32 additions & 0 deletions sfdx-source/apex-common/test/classes/fflib_SObjectsTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,38 @@ private class fflib_SObjectsTest
);
}

@IsTest
static void itShouldReturnRecordsByIds()
{
final Id accountIdA = fflib_IDGenerator.generate(Schema.Account.SObjectType);
final Id accountIdB = fflib_IDGenerator.generate(Schema.Account.SObjectType);
final Id accountIdC = fflib_IDGenerator.generate(Schema.Account.SObjectType);

fflib_SObjects domain = new fflib_SObjects(
new List<SObject>
{
new Account(Id = accountIdA, Name = 'A'),
new Account(Id = accountIdB, Name = 'B'),
new Account(Id = accountIdC, Name = 'C')
});

Set<Id> idsToRetrieve = new Set<Id> {accountIdA, accountIdC};

// Get just a subset of the domain
List<SObject> subSetRecords = domain.getRecords(idsToRetrieve);
Set<Id> resultIds = new Map<Id, SObject>(subSetRecords).keySet();
System.assertEquals(2, subSetRecords.size(), 'Unexpected amount of record returned');
System.assert(resultIds.containsAll(idsToRetrieve), 'Did not retrieve the correct records');

// Get all the other records
List<SObject> allOtherRecords = domain.getRecordsNotIn(idsToRetrieve);
System.assertEquals(1, allOtherRecords.size(), 'Unexpected amount of record returned');
System.assertEquals(accountIdB, allOtherRecords.get(0).Id, 'Did not retrieve the correct record');

// Get a single specific record
System.assertEquals(accountIdB, domain.getRecord(accountIdB).Id, 'Incorrect record retrieved from domain');
}

@IsTest
static void itShouldReturnRecordsWithFieldValues()
{
Expand Down

0 comments on commit 9890745

Please sign in to comment.