@@ -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 ();
@@ -141,6 +196,66 @@ public virtual class fflib_SObjects
141196 return result ;
142197 }
143198
199+ /**
200+ * Get a map with the record mapped to the given Id field value
201+ * Key fields containing null values are omitted
202+ * This method is primarily intended for Id fields with a unique value.
203+ * If duplicate Id values exists the maps value will be overwritten.
204+ *
205+ * @param sObjectField The field to use as key for the map
206+ *
207+ * @return Returns a map with the record mapped to the given Id field value
208+ *
209+ * @example
210+ * Account account = Account.newInstance(records);
211+ * Map<Id, SObject> accountById = account.getRecordByIdField(Account.Id);
212+ */
213+ @TestVisible
214+ protected virtual Map <Id , SObject > getRecordByIdField (Schema.SObjectField sObjectField )
215+ {
216+ Map <Id , SObject > result = new Map <Id , SObject >();
217+ for (SObject record : getRecords ())
218+ {
219+ if (record .get (sObjectField ) == null ) continue ;
220+
221+ result .put ((Id ) record .get (sObjectField ), record );
222+ }
223+ return result ;
224+ }
225+
226+ /**
227+ * Get a map with the records mapped to the given Id field value
228+ * Key fields containing null values are omitted
229+ *
230+ * @param sObjectField The field to use as key for the map
231+ *
232+ * @return Returns a map with the records mapped to the given Id field value
233+ *
234+ * @example
235+ * Contacts contacts = Contacts.newInstance(records);
236+ * Map<Id, List<SObject>> contactsByAccountId = contacts.getRecordsByIdField(Contact.AccountId);
237+ */
238+ @TestVisible
239+ protected virtual Map <Id , List <SObject >> getRecordsByIdField (Schema.SObjectField sObjectField )
240+ {
241+ Map <Id , List <SObject >> result = new Map <Id , List <SObject >>();
242+ for (SObject record : getRecords ())
243+ {
244+ Id fieldId = (Id ) record .get (sObjectField );
245+ if (fieldId == null ) continue ;
246+
247+ if (result .containsKey (fieldId ))
248+ {
249+ result .get (fieldId ).add (record );
250+ }
251+ else
252+ {
253+ result .put (fieldId , new List <SObject > {record });
254+ }
255+ }
256+ return result ;
257+ }
258+
144259 /**
145260 * @param sObjectField The Schema.SObjectField to check its value for a Blank value
146261 *
0 commit comments