Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add overloaded method for register dirty with relationships #400

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 63 additions & 1 deletion sfdx-source/apex-common/main/classes/fflib_SObjectUnitOfWork.cls
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,37 @@ public virtual class fflib_SObjectUnitOfWork
registerRelationship(record, relatedToParentField, relatedToParentRecord);
}

/**
* Register newly created records to be inserted when commitWork is called,
* it also provides a reference to the single parent record instance (should also be registered as new separately)
*
* @param records newly created records of the same SObjectType to be inserted during commitWork
* @param relatedToParentField A SObjectField reference to the child field that associates the child record with its parent
* @param relatedToParentRecord A SObject instance of the parent record (should also be registered as new separately)
**/
public void registerNew(List<SObject> records, Schema.SObjectField relatedToParentField, SObject relatedToParentRecord)
{
if (records.isEmpty()) return;

// Only validate the first record, by definition of the method signature all records should be new and of the same type.
Schema.SObjectType sObjectType = records.get(0).getSObjectType();
String sObjectName = sObjectType.getDescribe().getName();
assertForNonEventSObjectType(sObjectName);
assertForSupportedSObjectType(m_newListByType, sObjectName);

for (SObject record : records)
{
if (record.getSObjectType() != sObjectType)
throw new UnitOfWorkException('All records should be of the same SObjectType');
if (record.Id != null)
throw new UnitOfWorkException('Only new records can be registered as new');

m_newListByType.get(sObjectName).add(record);
if (relatedToParentRecord != null && relatedToParentField != null)
registerRelationship(record, relatedToParentField, relatedToParentRecord);
}
}

/**
* Register a relationship between two records that have yet to be inserted to the database. This information will be
* used during the commitWork phase to make the references only when related records have been inserted to the database.
Expand Down Expand Up @@ -395,7 +426,7 @@ public virtual class fflib_SObjectUnitOfWork
* Register an existing record to be updated when commitWork is called,
* you may also provide a reference to the parent record instance (should also be registered as new separately)
*
* @param record A newly created SObject instance to be inserted during commitWork
* @param record An existing SObject instance to be updated during commitWork
* @param relatedToParentField A SObjectField reference to the child field that associates the child record with its parent
* @param relatedToParentRecord A SObject instance of the parent record (should also be registered as new separately)
**/
Expand All @@ -406,6 +437,37 @@ public virtual class fflib_SObjectUnitOfWork
registerRelationship(record, relatedToParentField, relatedToParentRecord);
}

/**
* Register existing records to be updated when commitWork is called,
* it provide a reference to the parent record instance (should also be registered as new separately)
*
* @param records Existing records to be updated during commitWork
* @param relatedToParentField A SObjectField reference to the child field that associates the child record with its parent
* @param relatedToParentRecord A SObject instance of the parent record (should also be registered as new separately)
**/
public void registerDirty(List<SObject> records, Schema.SObjectField relatedToParentField, SObject relatedToParentRecord)
{
if (records.isEmpty()) return;

// Only validate the first record, by definition of the method signature all records should be new and of the same type.
Schema.SObjectType sObjectType = records.get(0).getSObjectType();
String sObjectName = sObjectType.getDescribe().getName();
assertForNonEventSObjectType(sObjectName);
assertForSupportedSObjectType(m_dirtyMapByType, sObjectName);

for (SObject record : records)
{
if (record.getSObjectType() != sObjectType)
throw new UnitOfWorkException('All records should be of the same SObjectType');
if (record.Id == null)
throw new UnitOfWorkException('New records cannot be registered as dirty');

m_dirtyMapByType.get(sObjectName).put(record.Id, record);
if (relatedToParentRecord != null && relatedToParentField != null)
registerRelationship(record, relatedToParentField, relatedToParentRecord);
}
}

/**
* Register a list of existing records to be updated during the commitWork method
*
Expand Down