Answers.Body: <pre><code>CRM.api3('StateProvince', 'get', {
    &quot;sequential&quot;: 1,
    &quot;return&quot;: [&quot;name&quot;,&quot;country_id.name&quot;],
    &quot;options&quot;: {&quot;limit&quot;:0}
  })
  </code></pre>
  <p>UPDATE: I haven't tried this but <code>Lodash</code> v3 is loaded on most civi pages so you might be able to use <a href="https://lodash.com/docs/3.10.1#filter" rel="nofollow noreferrer">filter</a> or <a href="https://lodash.com/docs/3.10.1#forEach" rel="nofollow noreferrer">forEach</a> to combine the results of the above with the countryLimit array to remove inactive ones.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-23T18:37:29.373Z
Answers.Text: Thanks. You've helped me realize I left something out of my question. I need to limit the results by 'active' countries, which I think means in countryLimit. Your answer gives my all the StateProvince records in the database. I need to filter for active countries.
Answers.UserDisplayName: MikeSteigerwald
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-24T17:04:38.240Z
Answers.Text: Thanks, again. Would that involve a separate api call? I'm really trying to keep all this in a single call.
Answers.UserDisplayName: MikeSteigerwald
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-24T17:05:48.270Z
Answers.Text: Yes I think so since you'd need to get countryLimit separately.
Answers.UserDisplayName: Demerit
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-23T18:16:06.787Z
Answers.LastActivityDate: 2020-11-24T16:45:44.393Z
Answers.LastEditDate: 2020-11-24T16:45:44.393Z
Answers.UserDisplayName: Demerit
Answers.Body: <p>Adding another answer since this might work for you better. In your extension in hook_civicrm_buildForm do something like this. I'm not sure if this is documented anywhere but the country/state chain will do some autowiring for you if your fields are named in a special way.</p>
  <pre><code>function myextension_civicrm_buildForm($formName, &amp;$form) {
    if ($formName == 'The_Form_Name') {
      $form-&gt;add('select', 'country', ts('Country'), CRM_Core_PseudoConstant::country());
      $form-&gt;addChainSelect('state_province');
      CRM_Core_Region::instance('form-body')-&gt;add(array(
       'template' =&gt; 'CRM/Myextension/my.tpl',
      ));
    }
  </code></pre>
  <p>Then in CRM/Myextension/my.tpl something like:</p>
  <pre><code>&lt;tr&gt;&lt;td class=&quot;label&quot;&gt;{$form.country.label}&lt;/td&gt;
  &lt;td&gt;{$form.country.html}&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td class=&quot;label&quot;&gt;{$form.state_province.label}&lt;/td&gt;
  &lt;td&gt;{$form.state_province.html}&lt;/td&gt;&lt;/tr&gt;
  </code></pre>
  <p>You may have to fiddle a bit but this will automatically apply the countryLimit and connect the two fields.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-24T16:27:55.400Z
Answers.Text: Thanks, again, but I'm trying to keep this 100% JavaScript.
Answers.UserDisplayName: MikeSteigerwald
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-24T16:46:01.407Z
Answers.Text: Ok have updated the javascript one with an idea.
Answers.UserDisplayName: Demerit
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-24T00:12:17.103Z
Answers.LastActivityDate: 2020-11-24T00:12:17.103Z
Answers.UserDisplayName: Demerit
Body: <p>I would like to get all active (i.e., in countryLimit) states &amp; provinces available in my site for use in HTML tag. Can I query for that with a single JavaScript call to CRM.api3?</p>
  <p>I've tried:</p>
  <pre><code>CRM.api3('Setting', 'get', {
  </code></pre>
  <p>&quot;sequential&quot;: 1,
  &quot;return&quot;: [&quot;countryLimit&quot;],
  &quot;api.StateProvince.get&quot;: {}</p>
  <p>but that just gives me two unrelated arrays. Can I join them somehow?</p>
  
Tags: <api><javascript>
Title: How to get all active states & provinces from one api call?
ThreadText: # How to get all active states & provinces from one api call?
  
  (Asked by: username_0 on 2020-11-23)
  
  I would like to get all active (i.e., in countryLimit) states & provinces available in my site for use in HTML tag. Can I query for that with a single JavaScript call to CRM.api3?
  I've tried:
  CRM.api3('Setting', 'get', {
  
  "sequential": 1,
  "return": ["countryLimit"],
  "api.StateProvince.get": {}
  but that just gives me two unrelated arrays. Can I join them somehow?
  
  **Tags:** api, javascript
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2020-11-23. Score: 1
  
  CRM.api3('StateProvince', 'get', {
    "sequential": 1,
    "return": ["name","country_id.name"],
    "options": {"limit":0}
  })
  
  UPDATE: I haven't tried this but Lodash v3 is loaded on most civi pages so you might be able to use filter or forEach to combine the results of the above with the countryLimit array to remove inactive ones.
  
  > **Comments:**
  > **username_0**: Thanks. You've helped me realize I left something out of my question. I need to limit the results by 'active' countries, which I think means in countryLimit. Your answer gives my all the StateProvince records in the database. I need to filter for active countries.  
  > **username_0**: Thanks, again. Would that involve a separate api call? I'm really trying to keep all this in a single call.  
  > **username_1**: Yes I think so since you'd need to get countryLimit separately.  
  
  ---
  
  ### Answer by username_1 on 2020-11-24. Score: 0
  
  Adding another answer since this might work for you better. In your extension in hook_civicrm_buildForm do something like this. I'm not sure if this is documented anywhere but the country/state chain will do some autowiring for you if your fields are named in a special way.
  function myextension_civicrm_buildForm($formName, &$form) {
    if ($formName == 'The_Form_Name') {
      $form->add('select', 'country', ts('Country'), CRM_Core_PseudoConstant::country());
      $form->addChainSelect('state_province');
      CRM_Core_Region::instance('form-body')->add(array(
       'template' => 'CRM/Myextension/my.tpl',
      ));
    }
  
  Then in CRM/Myextension/my.tpl something like:
  {$form.country.label}
  {$form.country.html}
  {$form.state_province.label}
  {$form.state_province.html}
  
  You may have to fiddle a bit but this will automatically apply the countryLimit and connect the two fields.
  
  > **Comments:**
  > **username_0**: Thanks, again, but I'm trying to keep this 100% JavaScript.  
  > **username_1**: Ok have updated the javascript one with an idea.  
  
  ---
  
  