Skip to content

Commit

Permalink
Merge pull request #7165 from SalesforceFoundation/feature/fixBDIDupe…
Browse files Browse the repository at this point in the history
…MgmtInternalMatching

W-12250612 - Fixed issue with BDI matching when using Dupe Mgmt
  • Loading branch information
balsamhindi authored Feb 24, 2023
2 parents aa59b59 + e8bdf99 commit d5734a9
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 15 deletions.
31 changes: 22 additions & 9 deletions force-app/main/default/classes/BDI_ContactService.cls
Original file line number Diff line number Diff line change
Expand Up @@ -355,12 +355,14 @@ public with sharing class BDI_ContactService {
Boolean modelIsOneToOne = CAO_Constants.isOneToOne();

for (DataImport__c di : bdi.listDI) {

// skip di's that already have an error
if (di.Status__c == BDI_DataImport_API.bdiFailed)
continue;
List<String> listDiKey = listDIKeyCx(di, strCx);

Contact con = ContactFromDiKeys(listDiKey);

Boolean bothContactsSpecified = (strCx == 'Contact1' && isContactSpecified(di, 'Contact2')) ||
(strCx == 'Contact2' && isContactSpecified(di, 'Contact1'));

Expand All @@ -371,6 +373,7 @@ public with sharing class BDI_ContactService {
&& (con.Account.npe01__SYSTEM_AccountType__c != CAO_Constants.HH_ACCOUNT_TYPE);
Boolean usingAdvAccountOneToOne = usingAdv && contactAndAccountExist
&& (con.Account.npe01__SYSTEM_AccountType__c == CAO_Constants.ONE_TO_ONE_ORGANIZATION_TYPE);

// Only Contacts with Household Accounts are allowed, unless Advancement is installed,
// where One-to-one Accounts (Administrative in HEDA) are also valid
if (accountIsNotHousehold && !usingAdvAccountOneToOne) {
Expand Down Expand Up @@ -791,15 +794,20 @@ public with sharing class BDI_ContactService {
listFName.add(con.Firstname.toLowerCase());
addedToken = true;
}
if (!addedToken || !isFirstnameInContactMatchRules)

// Only add a blank space to the matching set if the field was null, or if the field wasn't in the matching
// method name AND Contact Duplicate Rules matching is not enabled. This is because adding a blank space
// allows it to make a match without the value from this field, which we do not want for internal matching when
// Contact Duplicate Rules (aka Duplicate Management) is the selected matching method.
if (!addedToken || (!isFirstnameInContactMatchRules && !isDuplicateManagement))
listFName.add('');

addedToken = false;
if (con.Lastname != null) {
listLName.add(con.Lastname.toLowerCase());
addedToken = true;
}
if (!addedToken || !isLastnameInContactMatchRules)
if (!addedToken || (!isLastnameInContactMatchRules && !isDuplicateManagement))
listLName.add('');

addedToken = false;
Expand All @@ -819,7 +827,7 @@ public with sharing class BDI_ContactService {
listEmail.add(con.npe01__AlternateEmail__c.toLowerCase());
addedToken = true;
}
if (!addedToken || !isEmailInContactMatchRules)
if (!addedToken || (!isEmailInContactMatchRules && !isDuplicateManagement))
listEmail.add('');

addedToken = false;
Expand Down Expand Up @@ -847,7 +855,7 @@ public with sharing class BDI_ContactService {
listPhone.add(con.npe01__WorkPhone__c);
addedToken = true;
}
if (!addedToken || !isPhoneInContactMatchRules)
if (!addedToken || (!isPhoneInContactMatchRules && !isDuplicateManagement))
listPhone.add('');

Set<String> setDiKey = new Set<String>();
Expand Down Expand Up @@ -917,7 +925,12 @@ public with sharing class BDI_ContactService {
listFName.add(String.valueOf(di.get(strCx + '_Firstname__c')).toLowerCase());
addedToken = true;
}
if (!isFirstnameInContactMatchRules || !addedToken) {

// Only add a blank space to the matching set if the field was null, or if the field wasn't in the matching
// method name AND Contact Duplicate Rules matching is not enabled. This is because adding a blank space
// allows it to make a match without the value from this field, which we do not want for internal matching when
// Contact Duplicate Rules (aka Duplicate Management) is the selected matching method.
if ((!isFirstnameInContactMatchRules && !isDuplicateManagement) || !addedToken) {
listFName.add('');
}

Expand All @@ -926,7 +939,7 @@ public with sharing class BDI_ContactService {
listLName.add(String.valueOf(di.get(strCx + '_Lastname__c')).toLowerCase());
addedToken = true;
}
if (!isLastnameInContactMatchRules || !addedToken) {
if ((!isLastnameInContactMatchRules && !isDuplicateManagement) || !addedToken) {
listLName.add('');
}

Expand All @@ -943,7 +956,7 @@ public with sharing class BDI_ContactService {
listEmail.add(String.valueOf(di.get(strCx + '_Alternate_Email__c')).toLowerCase());
addedToken = true;
}
if (!isEmailInContactMatchRules || !addedToken) {
if ((!isEmailInContactMatchRules && !isDuplicateManagement) || !addedToken) {
listEmail.add('');
}

Expand All @@ -964,7 +977,7 @@ public with sharing class BDI_ContactService {
listPhone.add(String.valueOf(di.get(strCx + '_Work_Phone__c')));
addedToken = true;
}
if (!isPhoneInContactMatchRules || !addedToken) {
if ((!isPhoneInContactMatchRules && !isDuplicateManagement) || !addedToken) {
listPhone.add('');
}

Expand Down
118 changes: 112 additions & 6 deletions force-app/main/default/classes/BDI_ContactService_TEST.cls
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,38 @@ private with sharing class BDI_ContactService_TEST {
Home_Street__c = '300 Fake Blvd',
Home_Zip_Postal_Code__c = '94105');

DataImport__c[] diList = new DataImport__c[]{testDi1,testDi2,testDi3};
// This record will test that it doesn't just match part of the contact data
DataImport__c testDi4 = new DataImport__c(
Contact1_Firstname__c = 'match1',
Contact1_Lastname__c = 'noMatch1',
Home_City__c = 'Fakeville',
Home_Country__c = 'United States',
Home_State_Province__c = 'California',
Home_Street__c = '300 Fake Blvd',
Home_Zip_Postal_Code__c = '94105');

DataImport__c testDi5 = new DataImport__c(
Contact1_Firstname__c = 'FirstInternalMatch1',
Contact1_Lastname__c = 'LastInternalMatch1',
Contact1_Personal_Email__c = '[email protected]',
Home_City__c = 'Fakeville',
Home_Country__c = 'United States',
Home_State_Province__c = 'California',
Home_Street__c = '300 Fake Blvd',
Home_Zip_Postal_Code__c = '94105');


DataImport__c testDi6 = new DataImport__c(
Contact1_Firstname__c = 'FirstInternalMatch1',
Contact1_Lastname__c = 'LastInternalMatch1',
Contact1_Personal_Email__c = '[email protected]',
Home_City__c = 'Fakeville',
Home_Country__c = 'United States',
Home_State_Province__c = 'California',
Home_Street__c = '300 Fake Blvd',
Home_Zip_Postal_Code__c = '94105');

DataImport__c[] diList = new DataImport__c[]{testDi1,testDi2,testDi3,testDi4,testDi5,testDi6};
insert diList;

// Instantiate the Data Import and Contact services so we can test the Contact Service in isolation
Expand Down Expand Up @@ -175,10 +206,12 @@ private with sharing class BDI_ContactService_TEST {
contService.dupeMgmtUtil = dupeMgmt;

Test.startTest();
// Run the tests with dry run turned on first.
contService.importContactsAndHouseholds();
Test.stopTest();


// Confirm that the Import Status and Imported Fields for each DI are updated appropriately.
// Confirm that the Import Status and Imported Fields for each DI are updated appropriately
// for Dry run
System.assertEquals(matchCont1.Id, testDi1.Contact1Imported__c);
System.assert(testDi1.Contact1ImportStatus__c.contains(match1c.duplicateRuleName));

Expand All @@ -189,14 +222,87 @@ private with sharing class BDI_ContactService_TEST {
System.assert(testDi2.Contact1ImportStatus__c.contains(match1c.duplicateRuleName));

System.assertEquals(null, testDi2.Contact2Imported__c);
System.assertEquals(diService.statusMatchedNew(), testDi2.Contact2ImportStatus__c);
System.assertEquals(System.Label.bdiDryRunNoMatch, testDi2.Contact2ImportStatus__c);

System.assertEquals(null, testDi3.Contact1Imported__c);
System.assertEquals(diService.statusMatchedNew(), testDi3.Contact1ImportStatus__c);
System.assertEquals(System.Label.bdiDryRunNoMatch, testDi3.Contact1ImportStatus__c);

System.assertEquals(null, testDi3.Contact2Imported__c);
System.assertEquals(diService.statusMatchedNew(), testDi3.Contact2ImportStatus__c);
System.assertEquals(System.Label.bdiDryRunNoMatch, testDi3.Contact2ImportStatus__c);

System.assertEquals(null, testDi4.Contact1Imported__c);
System.assertEquals(System.Label.bdiDryRunNoMatch,testDi4.Contact1ImportStatus__c);

System.assertEquals(null, testDi5.Contact1Imported__c);
System.assertEquals(System.Label.bdiDryRunNoMatch,testDi5.Contact1ImportStatus__c);

System.assertEquals(null, testDi6.Contact1Imported__c);
System.assertEquals(System.Label.bdiDryRunNoMatch,testDi6.Contact1ImportStatus__c);


// Now re-run the same records in normal mode (no dry run).

// Reset the status and Imported fields to simulate them not having already been matched.
for (DataImport__c di: diList) {
di.Contact1Imported__c = null;
di.Contact1ImportStatus__c = null;
di.Contact2Imported__c = null;
di.Contact2ImportStatus__c = null;
}

BDI_DataImportService diService2 = new BDI_DataImportService(false,mapService);
diService2.listDI = diList;
diService2.injectDataImportSettings(dis);
BDI_ContactService contService2 = new BDI_ContactService(diService2);
contService2.dupeMgmtUtil = dupeMgmt;

contService2.importContactsAndHouseholds();

Set<Id> existingContSet = new Set<Id>{matchCont1.Id,matchCont2.Id};

// DI contact should match on existing contact record
System.assertEquals(matchCont1.Id, testDi1.Contact1Imported__c);
System.assert(testDi1.Contact1ImportStatus__c.contains(match1c.duplicateRuleName));

// DI contact should match on existing contact record
System.assertEquals(matchCont2.Id, testDi1.Contact2Imported__c);
System.assert(testDi1.Contact2ImportStatus__c.contains(match2.duplicateRuleName));

// DI contact should match on existing contact record
System.assertEquals(matchCont1.Id, testDi2.Contact1Imported__c);
System.assert(testDi2.Contact1ImportStatus__c.contains(match1c.duplicateRuleName));

// DI should create contact since it doesn't match
System.assertNotEquals(null, testDi2.Contact2Imported__c);
System.assert(!existingContSet.contains(testDi2.Contact2Imported__c));
System.assertEquals(System.Label.bdiCreated, testDi2.Contact2ImportStatus__c);

// DI should create contact since it doesn't match
System.assertNotEquals(null, testDi3.Contact1Imported__c);
System.assert(!existingContSet.contains(testDi3.Contact1Imported__c));
System.assertEquals(System.Label.bdiCreated, testDi3.Contact1ImportStatus__c);

// DI should create contact since it doesn't match
System.assertNotEquals(null, testDi3.Contact2Imported__c);
System.assert(!existingContSet.contains(testDi3.Contact2Imported__c));
System.assertEquals(System.Label.bdiCreated, testDi3.Contact2ImportStatus__c);

// DI should create contact since it doesn't match
System.assertNotEquals(null, testDi4.Contact1Imported__c);
System.assert(!existingContSet.contains(testDi4.Contact1Imported__c));
System.assertEquals(System.Label.bdiCreated,testDi4.Contact1ImportStatus__c);

// DI should create contact since it doesn't match
System.assertNotEquals(null, testDi5.Contact1Imported__c);
System.assert(!existingContSet.contains(testDi5.Contact1Imported__c));
System.assertEquals(System.Label.bdiCreated,testDi5.Contact1ImportStatus__c);

// Confirm that the contact in the second identical DI is marked as matched,
// and has the same id as the first identical DI.
System.assertEquals(testDi5.Contact1Imported__c, testDi6.Contact1Imported__c);
System.assertEquals(System.Label.bdiMatched,testDi6.Contact1ImportStatus__c);

Test.stopTest();
}

/**
Expand Down

0 comments on commit d5734a9

Please sign in to comment.