Skip to content

Commit

Permalink
Changes in Addresses.cls and ContactAdapter.cls
Browse files Browse the repository at this point in the history
Added changes in Addresses.cls and ContactAdapter.cls to fix Contact Address Override Functionality
  • Loading branch information
salesforce-suyash-more committed Sep 13, 2024
1 parent 3c48e20 commit 8d1dd46
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
27 changes: 21 additions & 6 deletions force-app/main/adapter/in/sobjects/contact/ContactAdapter.cls
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
4 changes: 4 additions & 0 deletions force-app/main/domain/Addresses.cls
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 8d1dd46

Please sign in to comment.