7

I'm setting up our system to match up contacts with the geographical regions covered by our different chapters. I've gotten to the point of using the built in geocoding, along with polygon coordinates stored in a custom field for each chapter, to be able to create a relationship showing a contact "in the territory of" a chapter. I'll use hooks to create or remove the relationship if a contact address or chapter region changes.

I envisioned having multiple smart groups for each chapter - contacts who are "in the territory of" and members, or volunteers, or on the national mailing list, etc.

Is there a way to create smart groups via the API or DAO calls? It'd be ideal to create desired smart groups via a hook if a new chapter is added, rather than having to manually search on the desired parameters and create each group.

If not, maybe I'll create static groups from hooks, then have a cron job that syncs contacts to those groups. Basically implementing my own smart groups. Thoughts?

2 Answers 2

7

There's an example of creating a smart-group in v4.6's CRM_Contact_Form_Task::createHiddenGroup() which calls CRM_Contact_BAO_Group::createHiddenSmartGroup(). It should show the relevant fields/data-structures. There are a few details which would need to be adapted if used in another context:

  • In CRM_Contact_Form_Task::createHiddenGroup(), it just passes through some existing formValues. This data-structure is a big array defining the search parameters. You can inspect example records in civicrm_saved_search to see what this looks like. I believe this corresponds to advanced-search/CRM_Contact_BAO_Query, but you'd need to investigate.
  • In CRM_Contact_BAO_Group::createHiddenSmartGroup(), the title is hard-coded.
  • In CRM_Contact_BAO_Group::createHiddenSmartGroup(), the is_hidden field defaults to 1.
2

This can be done via the API.

Think of smart groups as a Group with an associated SavedSearch.

Step 1: Create a Saved Search by defining the fields select_tables, where_tables, and where_clause.

Step 2: Create a group and associate the SavedSearch you created above with the group using the saved_search_id field.

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.