500 error saving Search Kit Smart Group sorted on aggregated field
Upon saving a Search Kit Smart Group search sorted on an aggregated field, the server throws a 500 error (as seen in the browser console window) and the form continues to display the "Saving" status indefinitely.
Error occurs in Civi/Api4/Query/Api4SelectQuery.php::buildOrderBy() with
Invalid field 'GROUP_CONCAT_DISTINCT_Contact_ActivityContact_Activity_01_subject'
To recreate:
- Search | Search Kit: New Search
- Search for: Contacts.
- With Optional: Contact Activities If Contact Role = Activity Targets.
- Group By: Contact ID.
- Aggregate Fields: Subject: List Distinct.
- In the search result list, click the "Contact Activities: (List) Subject" header to sort by that column.
- Add: Smart Group.
- Enter titles for the Search Kit search and Smart Group
- Click Save.
- Observe "Saving" status, error in browser console and in Civi log file.
Civi 5.38.0. Confirmed on Demo site.