Skip to content

Commit 11d9de2

Browse files
✂️ Resolve relationships on upsert
1 parent 7ccdc3b commit 11d9de2

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

sfdx-source/apex-common/main/classes/fflib_SObjectUnitOfWork.cls

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,7 @@ public virtual class fflib_SObjectUnitOfWork
772772
String sobjName;
773773
for (Schema.SObjectType sObjectType : m_sObjectTypes) {
774774
sobjName = sObjectType.getDescribe().getName();
775+
m_relationships.get(sobjName).resolve();
775776
m_dml.dmlUpsert(m_upsertRecordsPerType.get(sobjName), m_externalIdToUpsertPerType.get(sobjName));
776777
}
777778
}
@@ -964,7 +965,8 @@ public virtual class fflib_SObjectUnitOfWork
964965

965966
public void resolve()
966967
{
967-
this.Record.put( this.RelatedToField, this.RelatedTo.Id);
968+
if (this.Record.get(this.RelatedToField) == null)
969+
this.Record.put( this.RelatedToField, this.RelatedTo.Id);
968970
}
969971
}
970972

sfdx-source/apex-common/test/classes/fflib_SObjectUnitOfWorkTest.cls

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,97 @@ private with sharing class fflib_SObjectUnitOfWorkTest
589589
System.assertEquals(1, [SELECT COUNT() FROM Opportunity WHERE StageName = 'Closed']);
590590
}
591591

592+
593+
@isTest
594+
private static void testRegisterUpsertByExternalIdParentWillResolve() {
595+
596+
Opportunity existingOpp = new Opportunity(Name = 'Existing Opportunity', StageName = 'Open', CloseDate = System.today());
597+
insert existingOpp;
598+
599+
existingOpp.StageName = 'Closed';
600+
601+
System.assertEquals(1, [SELECT COUNT() FROM Opportunity]);
602+
System.assertEquals(0, [SELECT COUNT() FROM Opportunity WHERE StageName = 'Closed']);
603+
604+
List<Schema.SObjectType> orderWithAccounts = new List<Schema.SObjectType>();
605+
orderWithAccounts.add(Account.SObjectType);
606+
orderWithAccounts.addAll(MY_SOBJECTS);
607+
608+
String accName = 'new account';
609+
Account newAccount = new Account(Name = accName);
610+
611+
Test.startTest();
612+
fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(orderWithAccounts);
613+
uow.registerNew(newAccount);
614+
uow.registerUpsert(existingOpp, Opportunity.Id, Opportunity.AccountId, newAccount);
615+
uow.commitWork();
616+
Test.stopTest();
617+
618+
System.assertEquals(1, [SELECT COUNT() FROM Opportunity]);
619+
620+
Opportunity updatedOpp = [SELECT StageName, Account.Name FROM Opportunity];
621+
System.assertEquals('Closed', updatedOpp.StageName);
622+
System.assertEquals(accName, updatedOpp.Account.Name );
623+
}
624+
625+
626+
@isTest
627+
private static void testRegisterUpsertByExternalIdChildWillResolve() {
628+
629+
Opportunity newOpportunity = new Opportunity(Name = 'Existing Opportunity', StageName = 'Closed', CloseDate = System.today());
630+
631+
Account existingAccount = new Account(Name = 'old account name');
632+
insert existingAccount;
633+
634+
List<Schema.SObjectType> orderWithAccounts = new List<Schema.SObjectType>();
635+
orderWithAccounts.add(Account.SObjectType);
636+
orderWithAccounts.addAll(MY_SOBJECTS);
637+
638+
String accName = 'new account';
639+
640+
existingAccount.Name = accName;
641+
642+
Test.startTest();
643+
fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(orderWithAccounts);
644+
uow.registerDirty(existingAccount);
645+
uow.registerUpsert(newOpportunity, Opportunity.Id, Opportunity.AccountId, existingAccount);
646+
uow.commitWork();
647+
Test.stopTest();
648+
649+
System.assertEquals(1, [SELECT COUNT() FROM Opportunity]);
650+
651+
Opportunity updatedOpp = [SELECT StageName, Account.Name FROM Opportunity];
652+
System.assertEquals('Closed', updatedOpp.StageName);
653+
System.assertEquals(accName, updatedOpp.Account.Name );
654+
}
655+
656+
657+
@isTest
658+
private static void testRegisterUpsertByExternalIdBothWillResolve() {
659+
660+
Opportunity newOpportunity = new Opportunity(Name = 'Existing Opportunity', StageName = 'Closed', CloseDate = System.today());
661+
662+
String accName = 'new account';
663+
Account newAccount = new Account(Name = accName);
664+
665+
List<Schema.SObjectType> orderWithAccounts = new List<Schema.SObjectType>();
666+
orderWithAccounts.add(Account.SObjectType);
667+
orderWithAccounts.addAll(MY_SOBJECTS);
668+
669+
Test.startTest();
670+
fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(orderWithAccounts);
671+
uow.registerNew(newAccount);
672+
uow.registerUpsert(newOpportunity, Opportunity.Id, Opportunity.AccountId, newAccount);
673+
uow.commitWork();
674+
Test.stopTest();
675+
676+
System.assertEquals(1, [SELECT COUNT() FROM Opportunity]);
677+
678+
Opportunity updatedOpp = [SELECT StageName, Account.Name FROM Opportunity];
679+
System.assertEquals('Closed', updatedOpp.StageName);
680+
System.assertEquals(accName, updatedOpp.Account.Name );
681+
}
682+
592683
/**
593684
* Assert that actual events exactly match expected events (size, order and name)
594685
* and types match expected types

0 commit comments

Comments
 (0)