@@ -74,6 +74,60 @@ public virtual class fflib_SObjects
7474 return SObjectDescribe .getSObjectType ();
7575 }
7676
77+ /**
78+ * Adds an error message to the records in the domain
79+ *
80+ * @param message The error message to add to each record
81+ */
82+ protected void addError (String message )
83+ {
84+ for (SObject record : getRecords ())
85+ {
86+ record .addError (error (message , record ));
87+ }
88+ }
89+
90+ /**
91+ * Adds an error message to the a field records in the domain
92+ *
93+ * @param field The field where the error should be reported
94+ * @param message The error message to add to the given field on each record
95+ */
96+ @TestVisible
97+ protected virtual void addError (Schema.SObjectField field , String message )
98+ {
99+ for (SObject record : getRecords ())
100+ {
101+ record .addError (field , error (message , record , field ));
102+ }
103+ }
104+
105+ /**
106+ * Clear the field value on all the records of the domain
107+ * @param field The field to nullify
108+ */
109+ @TestVisible
110+ protected virtual void clearField (Schema.SObjectField field )
111+ {
112+ clearFields (new Set <Schema .SObjectField >{ field });
113+ }
114+
115+ /**
116+ * Clear the field values on all the records of the domain
117+ * @param fields The fields to nullify
118+ */
119+ @TestVisible
120+ protected virtual void clearFields (Set <Schema .SObjectField > fields )
121+ {
122+ for (SObject record : getRecords ())
123+ {
124+ for (SObjectField field : fields )
125+ {
126+ record .put (field , null );
127+ }
128+ }
129+ }
130+
77131 /**
78132 * Ensures logging of errors in the Domain context for later assertions in tests
79133 *
@@ -96,44 +150,82 @@ public virtual class fflib_SObjects
96150 *
97151 * @return Returns the Error message
98152 **/
99- protected virtual String error (String message , SObject record , SObjectField field )
153+ protected virtual String error (String message , SObject record , Schema. SObjectField field )
100154 {
101155 return fflib_SObjects .Errors .error (this , message , record , field );
102156 }
103157
104- protected virtual Set <Object > getFieldValues (Schema.SObjectField sObjectField )
158+ /**
159+ * @param field The SObjectField reference of the type Id
160+ *
161+ * @return Return a set with all the Id values of the given field
162+ */
163+ @TestVisible
164+ protected Set <Id > getIdFieldValues (Schema.SObjectField field )
165+ {
166+ Set <Id > result = new Set <Id >();
167+ for (SObject record : getRecords ())
168+ {
169+ result .add ((Id ) record .get (field ));
170+ }
171+ return result ;
172+ }
173+
174+ /**
175+ * @param field The SObjectField reference of the type String
176+ *
177+ * @return Return a set with all the String values of the given field
178+ */
179+ @TestVisible
180+ protected Set <String > getStringFieldValues (Schema.SObjectField field )
181+ {
182+ Set <String > result = new Set <String >();
183+ for (SObject record : getRecords ())
184+ {
185+ result .add ((String ) record .get (field ));
186+ }
187+ return result ;
188+ }
189+
190+ /**
191+ * @param field The SObjectField reference
192+ *
193+ * @return Return a set with all the values of the given field
194+ */
195+ @TestVisible
196+ protected virtual Set <Object > getFieldValues (Schema.SObjectField field )
105197 {
106198 Set <Object > result = new Set <Object >();
107199 for (SObject record : getRecords ())
108200 {
109- result .add (record .get (sObjectField ));
201+ result .add (record .get (field ));
110202 }
111203 return result ;
112204 }
113205
114206 /**
115- * @param sObjectField The Schema.SObjectField to compare against the given value
116- * @param value The given value of the records sObjectField to include in the return
207+ * @param field The Schema.SObjectField to compare against the given value
208+ * @param value The given value of the records field to include in the return
117209 *
118- * @return A list with only the SObjects where the given sObjectField has the provided value
210+ * @return A list with only the SObjects where the given field has the provided value
119211 */
120- protected virtual List <SObject > getRecordsByFieldValue (Schema.SObjectField sObjectField , Object value )
212+ protected virtual List <SObject > getRecordsByFieldValue (Schema.SObjectField field , Object value )
121213 {
122- return getRecordsByFieldValues (sObjectField , new Set <Object >{value });
214+ return getRecordsByFieldValues (field , new Set <Object >{value });
123215 }
124216
125217 /**
126- * @param sObjectField The Schema.SObjectField to compare against the given value
127- * @param values The given values of the records sObjectField to include in the return
218+ * @param field The Schema.SObjectField to compare against the given value
219+ * @param values The given values of the records field to include in the return
128220 *
129- * @return A list with only the SObjects where the given sObjectField value is part of the provided values
221+ * @return A list with only the SObjects where the given field value is part of the provided values
130222 */
131- protected virtual List <SObject > getRecordsByFieldValues (Schema.SObjectField sObjectField , Set <Object > values )
223+ protected virtual List <SObject > getRecordsByFieldValues (Schema.SObjectField field , Set <Object > values )
132224 {
133225 List <SObject > result = new List <SObject >();
134226 for (SObject record : getRecords ())
135227 {
136- if (values ?. contains (record .get (sObjectField )))
228+ if (values ?. contains (record .get (field )))
137229 {
138230 result .add (record );
139231 }
@@ -142,53 +234,52 @@ public virtual class fflib_SObjects
142234 }
143235
144236 /**
145- * @param sObjectField The Schema.SObjectField to check its value for a Blank value
237+ * @param field The Schema.SObjectField to check its value for a Blank value
146238 *
147- * @return A list with only the SObjects where the given sObjectField value is either null or '')
239+ * @return A list with only the SObjects where the given field value is either null or '')
148240 */
149- protected virtual List <SObject > getRecordsWithBlankFieldValues (Schema.SObjectField sObjectField )
241+ protected virtual List <SObject > getRecordsWithBlankFieldValues (Schema.SObjectField field )
150242 {
151243 return getRecordsWithBlankFieldValues (
152- new Set <Schema .SObjectField > {sObjectField }
244+ new Set <Schema .SObjectField > {field }
153245 );
154246 }
155247
156248 /**
157- * @param sObjectFields The Schema.SObjectFields to check their value for a Blank value
249+ * @param fields The Schema.SObjectFields to check their value for a Blank value
158250 *
159- * @return A list with only the SObjects where the at least one given sObjectField value is either null or '')
251+ * @return A list with only the SObjects where the at least one given field value is either null or '')
160252 */
161- protected virtual List <SObject > getRecordsWithBlankFieldValues (Set <Schema .SObjectField > sObjectFields )
253+ protected virtual List <SObject > getRecordsWithBlankFieldValues (Set <Schema .SObjectField > fields )
162254 {
163255 List <SObject > result = new List <SObject >();
164256 for (SObject record : getRecords ())
165257 {
166- for (SObjectField sObjectField : sObjectFields )
258+ for (SObjectField field : fields )
167259 {
168- if (String .isBlank ((String ) record .get (sObjectField )))
169- {
170- result .add (record );
171- break ;
172- }
260+ if (String .isNotBlank ((String ) record .get (field ))) continue ;
261+
262+ result .add (record );
263+ break ;
173264 }
174265 }
175266 return result ;
176267 }
177268
178269 /**
179- * @param sObjectFields The Schema.SObjectFields to check their value for a Blank value
270+ * @param fields The Schema.SObjectFields to check their value for a Blank value
180271 *
181- * @return A list with only the SObjects where all given sObjectField values are either null or ''
272+ * @return A list with only the SObjects where all given field values are either null or ''
182273 */
183- protected virtual List <SObject > getRecordsWithAllBlankFieldValues (Set <Schema .SObjectField > sObjectFields )
274+ protected virtual List <SObject > getRecordsWithAllBlankFieldValues (Set <Schema .SObjectField > fields )
184275 {
185276 List <SObject > result = new List <SObject >();
186277 for (SObject record : getRecords ())
187278 {
188279 Boolean allBlank = true ;
189- for (SObjectField sObjectField : sObjectFields )
280+ for (SObjectField field : fields )
190281 {
191- if (String .isNotBlank ((String ) record .get (sObjectField )))
282+ if (String .isNotBlank ((String ) record .get (field )))
192283 {
193284 allBlank = false ;
194285 break ;
@@ -200,30 +291,30 @@ public virtual class fflib_SObjects
200291 }
201292
202293 /**
203- * @param sObjectField The Schema.SObjectField to check its value for a Non-Blank value
294+ * @param field The Schema.SObjectField to check its value for a Non-Blank value
204295 *
205- * @return A list with only the SObjects where the given sObjectField value is not null or ''
296+ * @return A list with only the SObjects where the given field value is not null or ''
206297 */
207- protected virtual List <SObject > getRecordsWithNotBlankFieldValues (Schema.SObjectField sObjectField )
298+ protected virtual List <SObject > getRecordsWithNotBlankFieldValues (Schema.SObjectField field )
208299 {
209300 return getRecordsWithNotBlankFieldValues (
210- new Set <Schema .SObjectField > {sObjectField }
301+ new Set <Schema .SObjectField > {field }
211302 );
212303 }
213304
214305 /**
215- * @param sObjectFields The Schema.SObjectFields to check their value for a Non-Blank value
306+ * @param fields The Schema.SObjectFields to check their value for a Non-Blank value
216307 *
217- * @return A list with only the SObjects where the at least one given sObjectField value not null or ''
308+ * @return A list with only the SObjects where the at least one given field value not null or ''
218309 */
219- protected virtual List <SObject > getRecordsWithNotBlankFieldValues (Set <Schema .SObjectField > sObjectFields )
310+ protected virtual List <SObject > getRecordsWithNotBlankFieldValues (Set <Schema .SObjectField > fields )
220311 {
221312 List <SObject > result = new List <SObject >();
222313 for (SObject record : getRecords ())
223314 {
224- for (SObjectField sObjectField : sObjectFields )
315+ for (SObjectField field : fields )
225316 {
226- if (String .isNotBlank ((String ) record .get (sObjectField )))
317+ if (String .isNotBlank ((String ) record .get (field )))
227318 {
228319 result .add (record );
229320 break ;
@@ -234,19 +325,19 @@ public virtual class fflib_SObjects
234325 }
235326
236327 /**
237- * @param sObjectFields The Schema.SObjectFields to check their value for a Non-Blank value
328+ * @param fields The Schema.SObjectFields to check their value for a Non-Blank value
238329 *
239- * @return A list with only the SObjects where all given sObjectField values are not null or ''
330+ * @return A list with only the SObjects where all given field values are not null or ''
240331 */
241- protected virtual List <SObject > getRecordsWithAllNotBlankFieldValues (Set <Schema .SObjectField > sObjectFields )
332+ protected virtual List <SObject > getRecordsWithAllNotBlankFieldValues (Set <Schema .SObjectField > fields )
242333 {
243334 List <SObject > result = new List <SObject >();
244335 for (SObject record : getRecords ())
245336 {
246337 Boolean allNonBlank = true ;
247- for (SObjectField sObjectField : sObjectFields )
338+ for (SObjectField field : fields )
248339 {
249- if (String .isBlank ((String ) record .get (sObjectField )))
340+ if (String .isBlank ((String ) record .get (field )))
250341 {
251342 allNonBlank = false ;
252343 break ;
@@ -257,31 +348,36 @@ public virtual class fflib_SObjects
257348 return result ;
258349 }
259350
260-
261- protected virtual void setFieldValue (Schema.SObjectField sObjectField , Object value )
351+ /**
352+ * Modifies a value of a field for all records in the domain
353+ *
354+ * @param field The reference to the SObjectField to be modified
355+ * @param value The value to store in the given SObjectField
356+ */
357+ protected virtual void setFieldValue (Schema.SObjectField field , Object value )
262358 {
263359 for (SObject record : getRecords ())
264360 {
265- record .put (sObjectField , value );
361+ record .put (field , value );
266362 }
267363 }
268364
269365 /**
270- * @param sObjectFieldToCheck The SObjectField to match the key against in the provided map
271- * @param sObjectFieldToUpdate The SObjectField to store the mapped value when the key matches the value in the sObjectFieldToUpdate field
272- * @param values Map of values to store by the sObjectFieldToCheck fields value
366+ * @param fieldToCheck The SObjectField to match the key against in the provided map
367+ * @param fieldToUpdate The SObjectField to store the mapped value when the key matches the value in the fieldToUpdate field
368+ * @param values Map of values to store by the fieldToCheck fields value
273369 */
274370 protected virtual void setFieldValueByMap (
275- Schema.SObjectField sObjectFieldToCheck ,
276- Schema.SObjectField sObjectFieldToUpdate ,
371+ Schema.SObjectField fieldToCheck ,
372+ Schema.SObjectField fieldToUpdate ,
277373 Map <Object , Object > values )
278374 {
279375 for (SObject record : getRecords ())
280376 {
281- Object keyValue = record .get (sObjectFieldToCheck );
377+ Object keyValue = record .get (fieldToCheck );
282378 if (values ?. containsKey (keyValue ))
283379 {
284- record .put (sObjectFieldToUpdate , values .get (keyValue ));
380+ record .put (fieldToUpdate , values .get (keyValue ));
285381 }
286382 }
287383 }
0 commit comments