SalesforceFoundation/NPSP

Default Primary Address Type with Blank Address Fields Cause Blank Address Record to be Inserted and Deleted

jesslopez-sf opened this issue · 2 comments

If a customer creates a Contact that has a value in the Primary Address Type field (npe01__Primary_Address_Type__c) but no data in any Mailing Address fields, NPSP will still create a blank Address record and then delete it, causing unnecessary SOQL queries and DML operations. Some customers have set a default picklist value on this field, so the possibility of this scenario is higher when they insert Contacts declaratively and programmatically.

Steps to Repeat

  • Go to Setup --> Object Manager --> Contact --> Fields & Relationships --> Primary Address Type. Edit the "Home" Picklist Value and set it as a default.
  • Create a Contact with a First Name and Last Name. The Primary Address Type will default to "Home". Click Save.

Root Cause

When ADDR_Contact_TDTM runs in AfterInsert context, we call ContactAdapter.onAfterInsert(), which calls createAddressesForContacts(). We loop through each Contact in getAddressCreationQueueInAfterContext() and we check to see if isAddressChanged(),isMovingToNewHoushold() or if undeliverable status changed. In isAddressChanged(), the ContactService.isContactAddressChanged() method returns a boolean

return (!isContactAddressEmpty(con1) ||
con1.npe01__Primary_Address_Type__c != null);)

isContactAddressEmpty() returns true since all mailing fields are blank, but the second line of the return statement is true because of the default picklist value, so we ultimately add the Contact to the address creation queue for insertion.

Limits Comparison

In 3.209, with a default picklist value set on Primary Address Type and no mailing address fields populated:

  • 27 SOQL queries
  • 5 DML Statements (3 are unnecessary - the address insert, address delete, and update)
  • 5 DML rows

In 3.209, with a blank Primary Address Type and no mailing address fields populated:

  • 17 SOQL queries
  • 2 DML statements
  • 2 DML rows

This behavior was also present in 3.208, but order of execution was different. There was an additional DML update and 2 fewer SOQL queries.

  • 25 SOQL queries
  • 6 DML statements
  • 6 DML rows

Workaround

If you have a default picklist value configured on the Primary Address Type field and are experiencing any npsp SOQL query or DML limits being hit in unit tests or in other areas where you may insert Contacts with no mailing address fields populated, clear the value in the npe01__Primary_Address_Type__c field. (In the UI, set the value to --None--; programmatically, set the value to '').

W-10252900

Please follow this Known Issue in the Trailblazer Community for updates.