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?

2 Answers 2

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.

  • 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. Commented Nov 23, 2020 at 18:37
  • Thanks, again. Would that involve a separate api call? I'm really trying to keep all this in a single call. Commented Nov 24, 2020 at 17:04
  • Yes I think so since you'd need to get countryLimit separately.
    – Demerit
    Commented Nov 24, 2020 at 17:05

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());
     'template' => 'CRM/Myextension/my.tpl',

Then in CRM/Myextension/my.tpl something like:

<tr><td class="label">{$form.country.label}</td>
<tr><td class="label">{$form.state_province.label}</td>

You may have to fiddle a bit but this will automatically apply the countryLimit and connect the two fields.

  • Thanks, again, but I'm trying to keep this 100% JavaScript. Commented Nov 24, 2020 at 16:27
  • Ok have updated the javascript one with an idea.
    – Demerit
    Commented Nov 24, 2020 at 16:46

Your Answer

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

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