Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,7 @@ public virtual class fflib_SObjectUnitOfWork
{
for (Schema.SObjectType sObjectType : m_sObjectTypes)
{
m_relationships.get(sObjectType.getDescribe().getName()).resolve();
m_dml.dmlUpdate(m_dirtyMapByType.get(sObjectType.getDescribe().getName()).values());
}
}
Expand Down Expand Up @@ -897,7 +898,9 @@ public virtual class fflib_SObjectUnitOfWork

public void resolve()
{
this.Record.put( this.RelatedToField, this.RelatedTo.Id);
if (this.Record.get(this.RelatedToField) == null){
this.Record.put( this.RelatedToField, this.RelatedTo.Id);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,65 @@ private with sharing class fflib_SObjectUnitOfWorkTest
);
}

@IsTest
private static void testRegisterDirtyRelatedToNewSobject() {
// GIVEN an existing opportunity
Opportunity existingOpp = new Opportunity(
Id = fflib_IDGenerator.generate(Schema.Opportunity.SObjectType),
Name = 'Existing Opportunity',
StageName = 'Closed',
CloseDate = System.today()
);
// AND a new Account to which the existing opportunity will be related to
Account newAccount = new Account(
Name = 'New Account'
);

// WHEN
Test.startTest();
MockDML mockDML = new MockDML();
List<Schema.SObjectType> mySobjects = new List<Schema.SObjectType>{
Account.SObjectType,
Opportunity.SObjectType
};
fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(mySobjects, mockDML);
uow.registerNew(newAccount);
uow.registerDirty(existingOpp, Opportunity.AccountId, newAccount);
uow.commitWork();
Test.stopTest();

// THEN
System.assert(
new fflib_MatcherDefinitions.SObjectsWith(
new List<Map<SObjectField, Object>>{
new Map<SObjectField, Object>
{
Account.Id => newAccount.Id,
Account.Name => 'New Account'
}
}
).matches(mockDML.recordsForInsert),
'The new accoout record does not match'
);

// AND

System.assert(
new fflib_MatcherDefinitions.SObjectsWith(
new List<Map<SObjectField, Object>>{
new Map<SObjectField, Object>
{
Opportunity.Id => existingOpp.Id,
Opportunity.Name => 'Existing Opportunity',
Opportunity.StageName => 'Closed',
Opportunity.AccountId => newAccount.Id
}
}
).matches(mockDML.recordsForUpdate),
'The opportunity record should be related to the new Account'
);
}

@IsTest
private static void testRegisterUpsert() {
Opportunity existingOpp = new Opportunity(
Expand Down