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

bugfix for Issue 221 #449

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
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