@@ -280,6 +280,37 @@ public virtual class fflib_SObjectUnitOfWork
280280 registerRelationship (record , relatedToParentField , relatedToParentRecord );
281281 }
282282
283+ /**
284+ * Register newly created records to be inserted when commitWork is called,
285+ * it also provides a reference to the single parent record instance (should also be registered as new separately)
286+ *
287+ * @param records newly created records of the same SObjectType to be inserted during commitWork
288+ * @param relatedToParentField A SObjectField reference to the child field that associates the child record with its parent
289+ * @param relatedToParentRecord A SObject instance of the parent record (should also be registered as new separately)
290+ **/
291+ public void registerNew (List <SObject > records , Schema.SObjectField relatedToParentField , SObject relatedToParentRecord )
292+ {
293+ if (records .isEmpty ()) return ;
294+
295+ // Only validate the first record, by definition of the method signature all records should be new and of the same type.
296+ Schema .SObjectType sObjectType = records .get (0 ).getSObjectType ();
297+ String sObjectName = sObjectType .getDescribe ().getName ();
298+ assertForNonEventSObjectType (sObjectName );
299+ assertForSupportedSObjectType (m_newListByType , sObjectName );
300+
301+ for (SObject record : records )
302+ {
303+ if (record .getSObjectType () != sObjectType )
304+ throw new UnitOfWorkException (' All records should be of the same SObjectType' );
305+ if (record .Id != null )
306+ throw new UnitOfWorkException (' Only new records can be registered as new' );
307+
308+ m_newListByType .get (sObjectName ).add (record );
309+ if (relatedToParentRecord != null && relatedToParentField != null )
310+ registerRelationship (record , relatedToParentField , relatedToParentRecord );
311+ }
312+ }
313+
283314 /**
284315 * Register a relationship between two records that have yet to be inserted to the database. This information will be
285316 * used during the commitWork phase to make the references only when related records have been inserted to the database.
@@ -395,7 +426,7 @@ public virtual class fflib_SObjectUnitOfWork
395426 * Register an existing record to be updated when commitWork is called,
396427 * you may also provide a reference to the parent record instance (should also be registered as new separately)
397428 *
398- * @param record A newly created SObject instance to be inserted during commitWork
429+ * @param record An existing SObject instance to be updated during commitWork
399430 * @param relatedToParentField A SObjectField reference to the child field that associates the child record with its parent
400431 * @param relatedToParentRecord A SObject instance of the parent record (should also be registered as new separately)
401432 **/
@@ -406,6 +437,37 @@ public virtual class fflib_SObjectUnitOfWork
406437 registerRelationship (record , relatedToParentField , relatedToParentRecord );
407438 }
408439
440+ /**
441+ * Register existing records to be updated when commitWork is called,
442+ * it provide a reference to the parent record instance (should also be registered as new separately)
443+ *
444+ * @param records Existing records to be updated during commitWork
445+ * @param relatedToParentField A SObjectField reference to the child field that associates the child record with its parent
446+ * @param relatedToParentRecord A SObject instance of the parent record (should also be registered as new separately)
447+ **/
448+ public void registerDirty (List <SObject > records , Schema.SObjectField relatedToParentField , SObject relatedToParentRecord )
449+ {
450+ if (records .isEmpty ()) return ;
451+
452+ // Only validate the first record, by definition of the method signature all records should be new and of the same type.
453+ Schema .SObjectType sObjectType = records .get (0 ).getSObjectType ();
454+ String sObjectName = sObjectType .getDescribe ().getName ();
455+ assertForNonEventSObjectType (sObjectName );
456+ assertForSupportedSObjectType (m_dirtyMapByType , sObjectName );
457+
458+ for (SObject record : records )
459+ {
460+ if (record .getSObjectType () != sObjectType )
461+ throw new UnitOfWorkException (' All records should be of the same SObjectType' );
462+ if (record .Id == null )
463+ throw new UnitOfWorkException (' New records cannot be registered as dirty' );
464+
465+ m_dirtyMapByType .get (sObjectName ).put (record .Id , record );
466+ if (relatedToParentRecord != null && relatedToParentField != null )
467+ registerRelationship (record , relatedToParentField , relatedToParentRecord );
468+ }
469+ }
470+
409471 /**
410472 * Register a list of existing records to be updated during the commitWork method
411473 *
0 commit comments