for(String accountId : accountIdSet){
if(newContactWithAccountMap.get(accountId) != null){
for(Contact newContact : newContactWithAccountMap.get(accountId)){
for(Contact oldContact : mapOfAccountIdWithItsContact.get(accountId)){
//Check for duplication only in the respective account, also this should not apply on insertion of Office contact
matchingContactFound = false;
if(oldContact.id != newContact.id){ //while insert, newContact's id will be null and while update it will verify that it is not matching itself with its old record.
for(String filterFieldName : mapOfAccountWithFilters.get(accountId)){
if(oldContact.get(filterFieldName) == newContact.get(filterFieldName)){
matchingContactFound = true;
//If match is found update last de duplication date to today on old contact
oldContact.Last_De_Duplication_Date__c = System.Today();
oldContactsToUpdateSet.add(oldContact);
}else{
matchingContactFound = false;
break; //get another "old contact"
}
}
}
if(matchingContactFound){
//stop it from being inserted
duplicateContactSet.add(newContact.Id);
//newContact.addError('Contact cannot be inserted because a contact is already present based on the Master Target Identifier at client level.');
break; //get another "new contact"
}
}
}
}
}
Any help in avoiding 4 loops or an alternate approach will be greatly appreciated. Thanks in advance.