Skip to content

Commit

Permalink
Added the ability to mark only certain fields as dirty when first add…
Browse files Browse the repository at this point in the history
…ing a record to the DML
  • Loading branch information
rob-baillie-ortoo committed Mar 30, 2022
1 parent 5e008f5 commit 868890c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -367,19 +367,26 @@ public virtual class fflib_SObjectUnitOfWork
{
if (record.Id == null)
throw new UnitOfWorkException('New records cannot be registered as dirty');
String sObjectType = record.getSObjectType().getDescribe().getName();

SobjectType oSobjectType = record.getSObjectType();
String sObjectType = oSobjectType.getDescribe().getName();

assertForNonEventSObjectType(sObjectType);
assertForSupportedSObjectType(m_dirtyMapByType, sObjectType);

// If record isn't registered as dirty, or no dirty fields to drive a merge
if (!m_dirtyMapByType.get(sObjectType).containsKey(record.Id) || dirtyFields.isEmpty())
{
// Register the record as dirty
m_dirtyMapByType.get(sObjectType).put(record.Id, record);
}
else
{
if ( dirtyFields.isEmpty() )
{
m_dirtyMapByType.get(sObjectType).put(record.Id, record);
}
else
{
if ( !m_dirtyMapByType.get(sObjectType).containsKey(record.Id) )
{
// If the record isn't already there, put a new skeleton one in there
m_dirtyMapByType.get(sObjectType).put(record.Id, oSobjectType.newSObject( record.id ) );
}

// Update the registered record's fields
SObject registeredRecord = m_dirtyMapByType.get(sObjectType).get(record.Id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,35 @@ private with sharing class fflib_SObjectUnitOfWorkTest
System.assertEquals('Expected', mockDML.recordsForUpdate.get(0).get(Schema.Opportunity.Name));
}

/**
* Try registering a single field as dirty on first call
*
* Testing:
*
* - only that field is updated
*/
@IsTest
private static void testRegisterDirtyOnce_field() {
Opportunity opp = new Opportunity(
Id = fflib_IDGenerator.generate(Schema.Opportunity.SObjectType),
Name = 'test name',
StageName = 'Open',
CloseDate = System.today());

Opportunity amountUpdate = new Opportunity(Id = opp.Id, Name = 'ShouldNotAppear', Amount = 250);
MockDML mockDML = new MockDML();
fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(MY_SOBJECTS, mockDML);
uow.registerDirty(amountUpdate, new List<SObjectField> { Opportunity.Amount } );
uow.commitWork();

System.assertEquals(1, mockDML.recordsForUpdate.size());

System.assertEquals(true, mockDML.recordsForUpdate.get(0).getPopulatedFieldsAsMap().containsKey( 'Amount' ));
System.assertEquals(false, mockDML.recordsForUpdate.get(0).getPopulatedFieldsAsMap().containsKey( 'Name' ));

System.assertEquals(amountUpdate.Amount, mockDML.recordsForUpdate.get(0).get(Schema.Opportunity.Amount));

}
/**
* Try registering a single field as dirty.
*
Expand Down

0 comments on commit 868890c

Please sign in to comment.