4

When I do Search->Find Contacts and get the standard list of contacts, it looks like the address displayed is not the contact's primary address, but simply the first address that was created for the contact. I've looked in the code to try to find where this query is located, but I can't find it. Shouldn't the search results screen display the primary address, primary email, and primary phone?

I know I can create a custom profile to change the search results screen, but I would think that the default screen should display the primary contact information by default.

2
  • Steps to reproduce: create a new contact with address "111 First St". Add a second address "222 Second St" and make it primary. Turn off "Search Primary Details Only". Now you can search for First St or Second St, but First St will always show up in the search results because it has a lower address ID and apparently the address records sort by record ID and only the first one is displayed on the screen.
    – MikeLab
    Jul 24, 2019 at 0:01
  • This also causes exports of address fields with type primary to return erroneous results. If you have "Search Primary Details Only" set to off, your export with primary address fields will actually give you the first-created address fields, not the primary ones: civicrm.stackexchange.com/questions/31936/…
    – Lars SG
    Sep 5, 2019 at 19:04

2 Answers 2

5

MikeLab, Welcome to CiviCRM SE.

The address to be displayed on search result (primary or non-primary) is based on the setting 'Search Primary Details Only'.

You can change the value of this setting by navigating to CiviCRM >> Administrator >> Customize Data and Screens >> Search Preferences.

If you set 'Search Primary Details Only' to yes then it search address or phone or email on primary, if no then it will search on all address or phone or email. The same logic also works on listing.

HTH

Pradeep

3
  • Oh, interesting Pradeep! My client who pointed this out indeed has "Search Primary Details Only" turned off. That makes a lot of sense based on my answer below, and explains why I couldn't replicate this in testing. I suppose the solution is some sort of ORDER BY statement. Jul 23, 2019 at 16:15
  • One of my users brought this to my attention and the contact record in question has several addresses. There is an address marked primary, but that's not the address shown in the search result. I believe the setting you're referring to determines if non-primary information is searched, but I don't think it determines what information is displayed on the results screen.
    – MikeLab
    Jul 23, 2019 at 20:22
  • I think so it does ;) Jul 23, 2019 at 21:01
1

A client recently pointed this out to me, and I started to trace it down, but couldn't replicate it in my simple tests, so I just gave them a custom profile. However, I found the place in the code the query is generated:

  • CRM_Contact_BAO_Query is where all the magic happens. In the initialize() method there's this line: $this->_fromClause = self::fromClause($this->_tables, NULL, NULL, $this->_primaryLocation, $this->_mode, $apiEntity);
  • After that line runs, $this->_fromClause is set. In my testing, that includes LEFT JOIN civicrm_address ON ( contact_a.id = civicrm_address.contact_id AND civicrm_address.is_primary = 1 ). This is the correct behavior, not what we're seeing.
  • I tried figuring out how to trigger it NOT getting the is_primary = 1 bit. This is where I ran out of steam. I'm 90% sure it's in getEntitySpecificJoins(), but it could be related to getPrimaryCondition().
3
  • OK. Thanks for giving me a place to start. I'll take a closer look later this evening.
    – MikeLab
    Jul 23, 2019 at 20:23
  • The problem is that there are multiple joined address records and only the first one is displayed. A way to fix this would be to sort the address records (or emails or phones) by is_primary DESC, so the primary records show up first and are displayed. I have looked at the code for several hours, but can't figure out where I would add that. Do you have any idea where I could add an "order by" to sort the results so the primary address is returned first?
    – MikeLab
    Jul 24, 2019 at 13:21
  • It's a good question. As you probably saw, the query is put together from its various clauses ($this->_select, $this->_from, etc.). I think we'd need to define a $this->_orderBy. There's precedent for this in CiviReport (in the CRM_Report_Form class) but it seems like a lot of work for relatively little payoff. Come talk to me on chat.civicrm.org (I'm @junglebird) if you want to tackle it, I can probably put in some effort on this. Jul 24, 2019 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.