Answers.Body: <p>There's an example of creating a smart-group in v4.6's <a href="https://github.com/civicrm/civicrm-core/blob/4.6/CRM/Contact/Form/Task.php#L493">CRM_Contact_Form_Task::createHiddenGroup()</a> which calls <a href="https://github.com/civicrm/civicrm-core/blob/4.6/CRM/Contact/BAO/Group.php#L633">CRM_Contact_BAO_Group::createHiddenSmartGroup()</a>. It should show the relevant fields/data-structures. There are a few details which would need to be adapted if used in another context:</p>
  
  <ul>
  <li>In <code>CRM_Contact_Form_Task::createHiddenGroup()</code>, it just passes through some existing <code>formValues</code>. This data-structure is a big array defining the search parameters. You can inspect example records in <code>civicrm_saved_search</code> to see what this looks like. I believe this corresponds to advanced-search/<code>CRM_Contact_BAO_Query</code>, but you'd need to investigate.</li>
  <li>In <code>CRM_Contact_BAO_Group::createHiddenSmartGroup()</code>, the title is hard-coded.</li>
  <li>In <code>CRM_Contact_BAO_Group::createHiddenSmartGroup()</code>, the <code>is_hidden</code> field defaults to <code>1</code>.</li>
  </ul>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-15T20:13:29.433Z
Answers.LastActivityDate: 2015-07-15T20:13:29.433Z
Answers.UserDisplayName: Tim Otten
Answers.Body: <p>This can be done via the API.</p>
  
  <p>Think of smart groups as a <code>Group</code> with an associated <code>SavedSearch</code>.</p>
  
  <p>Step 1: Create a Saved Search by defining the fields <code>select_tables</code>, <code>where_tables</code>, and <code>where_clause</code>.</p>
  
  <p>Step 2: Create a group and associate the SavedSearch you created above with the group using the <code>saved_search_id</code> field.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-18T15:36:04.060Z
Answers.LastActivityDate: 2018-06-18T20:27:53.473Z
Answers.LastEditDate: 2018-06-18T20:27:53.473Z
Answers.UserDisplayName: michaelmcandrew
Body: <p>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.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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?</p>
  
Tags: <smartgroups>
Title: Can I create a Smart Group programatically?
ThreadText: # Can I create a Smart Group programatically?
  
  (Asked by: username_0 on 2015-07-15)
  
  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?
  
  **Tags:** smartgroups
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2015-07-15. Score: 7
  
  There's an example of creating a smart-group in v4.6's [CRM\_Contact\_Form\_Task::createHiddenGroup()](https://github.com/civicrm/civicrm-core/blob/4.6/CRM/Contact/Form/Task.php#L493) which calls [CRM\_Contact\_BAO\_Group::createHiddenSmartGroup()](https://github.com/civicrm/civicrm-core/blob/4.6/CRM/Contact/BAO/Group.php#L633). 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`.
  
  
  ---
  
  ### Answer by username_2 on 2018-06-18. Score: 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.
  
  
  ---
  
  