Answers.Body: <p>As of January 2017, there is a StateProvince API in CiviCRM: see <a href="https://issues.civicrm.org/jira/browse/CRM-19688" rel="nofollow noreferrer">CRM-19688</a>.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-12-20T17:03:37.397Z
Answers.LastActivityDate: 2016-12-20T17:03:37.397Z
Answers.UserDisplayName: Jon G - Megaphone Tech
Answers.Body: <p>If there is no state/Province API you would have to create one? Assuming you put your import script into an extension, this is not very hard to do. Using civix you can do generate:api StateProvince Get and the codes files (which you then have to adjust) will be generated for you. If you want to know more check out <a href="http://wiki.civicrm.org/confluence/display/CRMDOC/Create+a+Module+Extension#CreateaModuleExtension-AddanAPIfunction" rel="nofollow">http://wiki.civicrm.org/confluence/display/CRMDOC/Create+a+Module+Extension#CreateaModuleExtension-AddanAPIfunction</a></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-15T16:59:53.473Z
Answers.Text: I have some time pressure right now, but if I find time in the end I will consider to create an extension of my importer.
Answers.UserDisplayName: woistjadefox
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-15T11:51:34.333Z
Answers.LastActivityDate: 2015-09-15T11:51:34.333Z
Answers.UserDisplayName: ErikH - CiviCooP
Answers.Body: <p>I ended up just calling the internal method. </p>
  
  <pre><code>return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_StateProvince',
    $state_id,
    'abbreviation'
  );
  </code></pre>
  
  <p>Takes advantage of a higher level of abstraction than sql so possibly more future proof, but probably not as future proof as using a designated API.</p>
  
  <p>Still seems like you can't find it in the API, although getOptions looks close <a href="https://issues.civicrm.org/jira/browse/CRM-15919" rel="nofollow">https://issues.civicrm.org/jira/browse/CRM-15919</a>. </p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-21T05:54:37.927Z
Answers.LastActivityDate: 2016-04-21T05:54:37.927Z
Answers.UserDisplayName: Interlated
Answers.Body: <p><a href="http://localhost:8080/drupal05122016/sites/all/modules/civicrm/extern/rest.php?entity=Address&amp;action=getoptions&amp;api_key=userkey&amp;key=sitekey&amp;json=" rel="nofollow noreferrer">http://localhost:8080/drupal05122016/sites/all/modules/civicrm/extern/rest.php?entity=Address&amp;action=getoptions&amp;api_key=userkey&amp;key=sitekey&amp;json=</a>{"sequential":1,"field":"state_province_id","country_id":1101}</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-12-20T13:49:25.343Z
Answers.Text: It would be helpful if you could edit your answer to provide more detail.  As it stands it doesn't really answer the question.
Answers.UserDisplayName: William Mortada
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-12-20T10:41:13.397Z
Answers.LastActivityDate: 2016-12-20T10:41:13.397Z
Answers.UserDisplayName: user4099
Answers.Body: <p>Alternative way is if you use the table civicrm_state_province and the CRM_Core_DAO Class, something like this:</p>
  
  <pre><code>$query = "SELECT id FROM civicrm_state_province WHERE country_id = %1 AND abbreviation = %2";
  $params = array(
    1 =&gt; array($countryId, 'Integer'),
    2 =&gt; array($abbreviationFromSource, 'String'));
  $stateProvinceId = CRM_Core_DAO::singleValueQuery($query, $params);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-15T11:55:43.603Z
Answers.LastActivityDate: 2015-09-15T11:55:43.603Z
Answers.UserDisplayName: ErikH - CiviCooP
Body: <p>I wrote an custom contact import script for our NGO. The old contact data only contains valid country / state abbreviation names. For example CH / ZH = Switzerland / Zürich. </p>
  
  <p>When I now want to create a new address over the API I don't see a way to fill out the 'state_province_id' field. </p>
  
  <p>I'm able to get the country id with this syntax: </p>
  
  <pre><code>$result = civicrm_api3('Country', 'getsingle', array(
          'sequential' =&gt; 1,
          'return' =&gt; "id",
          'iso_code' =&gt; $row["V"],
      ));
  
  
  if(isset($result["id"])){
     $params["api.address.create"][0]["country_id"] = $result["id"];
  }
  </code></pre>
  
  <p>But how can I do the same with the State/Province, since there is no  entity I can choose in the API?</p>
  
Tags: <api><import>
Title: Get state/provice id with API3
ThreadText: # Get state/provice id with API3
  
  (Asked by: username_0 on 2015-09-15)
  
  I wrote an custom contact import script for our NGO. The old contact data only contains valid country / state abbreviation names. For example CH / ZH = Switzerland / Zürich. 
  When I now want to create a new address over the API I don't see a way to fill out the 'state_province_id' field. 
  I'm able to get the country id with this syntax: 
  $result = civicrm_api3('Country', 'getsingle', array(
          'sequential' => 1,
          'return' => "id",
          'iso_code' => $row["V"],
      ));
  
  
  if(isset($result["id"])){
     $params["api.address.create"][0]["country_id"] = $result["id"];
  }
  
  But how can I do the same with the State/Province, since there is no  entity I can choose in the API?
  
  **Tags:** api, import
  
  
  ---
  
  ## 5 answers
  
  ### Answer by username_2 on 2015-09-15. Score: 3 | Accepted answer
  
  Alternative way is if you use the table civicrm_state_province and the CRM_Core_DAO Class, something like this:
  $query = "SELECT id FROM civicrm_state_province WHERE country_id = %1 AND abbreviation = %2";
  $params = array(
    1 => array($countryId, 'Integer'),
    2 => array($abbreviationFromSource, 'String'));
  $stateProvinceId = CRM_Core_DAO::singleValueQuery($query, $params);
  
  
  ---
  
  ### Answer by username_1 on 2016-12-20. Score: 3
  
  As of January 2017, there is a StateProvince API in CiviCRM: see [CRM-19688](https://issues.civicrm.org/jira/browse/CRM-19688).
  
  
  ---
  
  ### Answer by username_2 on 2015-09-15. Score: 3
  
  If there is no state/Province API you would have to create one? Assuming you put your import script into an extension, this is not very hard to do. Using civix you can do generate:api StateProvince Get and the codes files (which you then have to adjust) will be generated for you. If you want to know more check out <http://wiki.civicrm.org/confluence/display/CRMDOC/Create+a+Module+Extension#CreateaModuleExtension-AddanAPIfunction>
  
  > **Comments:**
  > **username_0**: I have some time pressure right now, but if I find time in the end I will consider to create an extension of my importer.  
  
  ---
  
  ### Answer by username_3 on 2016-04-21. Score: 3
  
  I ended up just calling the internal method. 
  return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_StateProvince',
    $state_id,
    'abbreviation'
  );
  
  Takes advantage of a higher level of abstraction than sql so possibly more future proof, but probably not as future proof as using a designated API.
  Still seems like you can't find it in the API, although getOptions looks close https://issues.civicrm.org/jira/browse/CRM-15919.
  
  
  ---
  
  ### Answer by username_4 on 2016-12-20. Score: -3
  
  <http://localhost:8080/drupal05122016/sites/all/modules/civicrm/extern/rest.php?entity=Address&action=getoptions&api_key=userkey&key=sitekey&json=>{"sequential":1,"field":"state\_province\_id","country\_id":1101}
  
  > **Comments:**
  > **username_5**: It would be helpful if you could edit your answer to provide more detail.  As it stands it doesn't really answer the question.  
  
  ---
  
  