From 8d1dd4682dbc70562edc97942773bfa074095b5d Mon Sep 17 00:00:00 2001 From: Suyash More Date: Fri, 13 Sep 2024 18:56:19 +0530 Subject: [PATCH] Changes in Addresses.cls and ContactAdapter.cls Added changes in Addresses.cls and ContactAdapter.cls to fix Contact Address Override Functionality --- .../in/sobjects/contact/ContactAdapter.cls | 27 ++++++++++++++----- force-app/main/domain/Addresses.cls | 4 +++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/force-app/main/adapter/in/sobjects/contact/ContactAdapter.cls b/force-app/main/adapter/in/sobjects/contact/ContactAdapter.cls index 7309d27d0b7..205b86d54a8 100644 --- a/force-app/main/adapter/in/sobjects/contact/ContactAdapter.cls +++ b/force-app/main/adapter/in/sobjects/contact/ContactAdapter.cls @@ -665,21 +665,36 @@ public inherited sharing class ContactAdapter extends fflib_SObjects2 { Address__c newAddressFromContact = contactAddressesByContact.get(contact); Address__c existingAddressFromContact = existingAddressesByAddress.get(newAddressFromContact); - // if found a match - if (contactAddressHasAddressMatch(existingAddressFromContact)) { + // Check if the contact has Address Override enabled + if (contact.is_Address_Override__c == true) { + // Create a new address record specific to this contact + Address__c newAddress = new Address__c(); + newAddress.Household_Account__c = contact.AccountId; // Link to household/account + newAddress.MailingStreet__c = contact.MailingStreet; + newAddress.MailingCity__c = contact.MailingCity; + newAddress.MailingState__c = contact.MailingState; + newAddress.MailingPostalCode__c = contact.MailingPostalCode; + newAddress.MailingCountry__c = contact.MailingCountry; + newAddress.Default_Address__c = false; // It's not a default address + + // Add the new address to be inserted + contactAddressesToInsertByContact.put(contact, newAddress); + + // Link the new address to the contact's Current Address field + contact.Current_Address__c = newAddress.Id; + } + else if (contactAddressHasAddressMatch(existingAddressFromContact)) { updateContactAddressFromExistingAddress(contact, existingAddressFromContact); // Prevent an address that was just inserted by the BeforeInsert trigger from being udpated // a second time by the AfterInsert trigger. } - - // no match found, and its an override just for this contact - else if (contact.is_Address_Override__c) { + else { // put it on the list of addresss to create now contactAddressesToInsertByContact.put(contact, newAddressFromContact); } } - + // Insert the new addresses created for contacts with Address Override insertContactAddresses(contactAddressesToInsertByContact); } diff --git a/force-app/main/domain/Addresses.cls b/force-app/main/domain/Addresses.cls index 71a316b9cb7..1694bc221ca 100644 --- a/force-app/main/domain/Addresses.cls +++ b/force-app/main/domain/Addresses.cls @@ -292,6 +292,10 @@ public inherited sharing class Addresses extends fflib_SObjects2 { NPSP_Contact npspContact = new NPSP_Contact(contact); Boolean shouldUpdateContact= false; + if (npspContact.hasAddressOverride()) { + continue; // Skip update for contacts with address override + } + // detect that the contact's current address was deleted. // and if so clear any override flag so it will get the default address. if (npspContact.currentAddress() == null) {