Non-primary details exported in primary-only exports, when the setting 'Search Primary Details Only' is set to 'no'
If you run an export with 'Export PRIMARY fields', but you have the setting 'Search Primary Details Only' set to 'No', the primary filter is not applied and you simply get the first available row in the table. This also happens when you select the 'Primary' option when exporting individual fields.
This definitely applies to postal addresses. I haven't tested emails but I think it'll happen there too.
It happens because of this line in CRM_Contact_BAO_Query: https://github.com/civicrm/civicrm-core/blob/a2540ad336faf7fb0218055a485c1a702200293f/CRM/Contact/BAO/Query.php#L446 which defaults to using the value from the setting. If this is set to no, the result SQL clause doesn't include 'is_primary = 1'.
CRM_Export_BAO_ExportProcessor creates the Query object here: https://github.com/civicrm/civicrm-core/blob/a2540ad336faf7fb0218055a485c1a702200293f/CRM/Export/BAO/ExportProcessor.php#L509 but doesn't pass through a value for 'primaryLocationOnly' to override the default setting.
I can presumably adapt the above to pass through primaryLocationOnly when users have selected the 'Export PRIMARY fields' option - that seems simple enough. I haven't had a chance to dig into how it works when the export specifies individual fields, though - that seems like it'd be more complicated.
To replicate on demo site:
- Create a new tmp group
- Find a contact with an existing primary address
- Add a new address for them, and set it to primary
- Add this contact to the tmp group
- Turn off the setting 'Search Primary Details Only'
- Search for all members of the tmp group, and export them using 'Export PRIMARY fields only'
- The contact's address in the CSV will be the original address, which is non-primary