Answers.Body: <p>The field name is <code>gender_id</code> not <code>gender</code>.</p>
  
  <p>For a bit of history and explanation:</p>
  
  <p>The contact api automatically takes a few fields (notably gender_id, prefix_id, suffix_id, state_province_id and a few others) and gives you the label as well as the id for convenience. However this is not consistent across all apis and is not something I would generally rely on.</p>
  
  <p>In v4.4 a lot of work was done to standardize option lists and the upshot is that 'create' type actions will now accept either the id or the name of any option (not the translated label).</p>
  
  <p>So <code>civicrm_api3('contact', 'create', ['id'=&gt;123, 'gender'=&gt;'Male'])</code> and <code>civicrm_api3('contact', 'create', ['id'=&gt;123, 'gender'=&gt;2])</code> will do the same thing.</p>
  
  <p>Come to think of it, it would be nice to accept either 'gender' or 'gender_id' since that's more consistent with the output. <a href="https://github.com/civicrm/civicrm-core/pull/6368" rel="nofollow">Here's a PR for that</a>.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-31T17:25:00.593Z
Answers.LastActivityDate: 2015-07-31T17:25:00.593Z
Answers.UserDisplayName: Coleman
Answers.Body: <p>Try using <code>gender_id</code> when creating the contact, rather than <code>gender</code>.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-31T15:19:47.620Z
Answers.LastActivityDate: 2015-07-31T15:19:47.620Z
Answers.UserDisplayName: John
Answers.Body: <p>I solved it by creating a lookup function (which can be used for any field that takes an id that you wouldn't know):</p>
  
  <pre><code>function getIdforLabel($entity, $field, $label) {
  
      $result = civicrm_api3($entity, 'getoptions', ['sequential' =&gt; 1, 'field' =&gt; $field]);
      if ($result['is_error']) {
        throw new \Exception("Failed to getoptions for $entity.$field");
      }
      foreach ($result['values'] as $result) {
        if ($result['value'] == $label) {
          return $result['key'];
        }
      }
      throw new \Exception("Supplied value, '$label' is not a valid option.");
  }
  </code></pre>
  
  <p>So then you can go:</p>
  
  <pre><code>$gender_id = getIdForLabel('contact', 'gender_id', 'Male');
  civicrm_api3('contact', 'create', ['id'=&gt;$contact_id, 'gender_id'=&gt;$gender_id]);
  </code></pre>
  
  <p>Clearly <a href="https://civicrm.stackexchange.com/a/4351/35">davejenx</a>'s answer is simpler :-)</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-31T15:19:54.243Z
Answers.LastActivityDate: 2015-07-31T15:19:54.243Z
Answers.LastEditDate: 2017-04-13T12:53:13.167Z
Answers.UserDisplayName: artfulrobot
Answers.Body: <p>Experimenting with the API explorer, I found that <code>'gender_id' =&gt; 'Male'</code> worked.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-31T15:22:08.163Z
Answers.Text: Do you know if this is a generalised case that works for all such ids (e.g. `country_id`)? Obvs I can test them one by one, but it would be useful to be able to rely on this behaviour.
Answers.UserDisplayName: artfulrobot
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-31T15:24:36.593Z
Answers.Text: The latest version of the API explorer should help, as the API should still be fairly consistent between version 4.4 and the latest release at the moment: http://dmaster.demo.civicrm.org/civicrm/api login demo/demo
Answers.UserDisplayName: John
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-31T16:09:08.067Z
Answers.Text: Thanks, John. Yes the new API explorer is v nice. [related question](http://civicrm.stackexchange.com/questions/4355/how-to-set-a-custom-country-field-using-the-api)
Answers.UserDisplayName: artfulrobot
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-31T15:18:55.493Z
Answers.LastActivityDate: 2015-07-31T15:18:55.493Z
Answers.UserDisplayName: davejenx
Body: <p>If I do a <code>civicrm_api3('contact', 'getsingle', ['id'=&gt;123])</code> I get lots of data, including <code>gender</code>.</p>
  
  <p>However when I try to set the gender like:</p>
  
  <pre><code>civicrm_api3('contact', 'create', ['id'=&gt;123, 'gender'=&gt;'Male'])
  </code></pre>
  
  <p>It reports no error, but neither does it do anything(!).</p>
  
  <p>(Nb. I'm using v4.4.16 LTS)</p>
  
Tags: <api>
Title: How to set a contact's gender using API?
ThreadText: # How to set a contact's gender using API?
  
  (Asked by: username_0 on 2015-07-31)
  
  If I do a `civicrm_api3('contact', 'getsingle', ['id'=>123])` I get lots of data, including `gender`.
  
  However when I try to set the gender like:
  
  ```
  civicrm_api3('contact', 'create', ['id'=>123, 'gender'=>'Male'])
  
  ```
  
  It reports no error, but neither does it do anything(!).
  
  (Nb. I'm using v4.4.16 LTS)
  
  **Tags:** api
  
  
  ---
  
  ## 4 answers
  
  ### Answer by username_3 on 2015-07-31. Score: 4 | Accepted answer
  
  Experimenting with the API explorer, I found that `'gender_id' => 'Male'` worked.
  
  > **Comments:**
  > **username_0**: Do you know if this is a generalised case that works for all such ids (e.g. `country_id`)? Obvs I can test them one by one, but it would be useful to be able to rely on this behaviour.  
  > **username_2**: The latest version of the API explorer should help, as the API should still be fairly consistent between version 4.4 and the latest release at the moment: http://dmaster.demo.civicrm.org/civicrm/api login demo/demo  
  > **username_0**: Thanks, username_2. Yes the new API explorer is v nice. [related question](http://civicrm.stackexchange.com/questions/4355/how-to-set-a-custom-country-field-using-the-api)  
  
  ---
  
  ### Answer by username_1 on 2015-07-31. Score: 3
  
  The field name is `gender_id` not `gender`.
  
  For a bit of history and explanation:
  
  The contact api automatically takes a few fields (notably gender\_id, prefix\_id, suffix\_id, state\_province\_id and a few others) and gives you the label as well as the id for convenience. However this is not consistent across all apis and is not something I would generally rely on.
  
  In v4.4 a lot of work was done to standardize option lists and the upshot is that 'create' type actions will now accept either the id or the name of any option (not the translated label).
  
  So `civicrm_api3('contact', 'create', ['id'=>123, 'gender'=>'Male'])` and `civicrm_api3('contact', 'create', ['id'=>123, 'gender'=>2])` will do the same thing.
  
  Come to think of it, it would be nice to accept either 'gender' or 'gender\_id' since that's more consistent with the output. [Here's a PR for that](https://github.com/civicrm/civicrm-core/pull/6368).
  
  
  ---
  
  ### Answer by username_2 on 2015-07-31. Score: 2
  
  Try using `gender_id` when creating the contact, rather than `gender`.
  
  
  ---
  
  ### Answer by username_0 on 2015-07-31. Score: 1
  
  I solved it by creating a lookup function (which can be used for any field that takes an id that you wouldn't know):
  
  ```
  function getIdforLabel($entity, $field, $label) {
  
      $result = civicrm_api3($entity, 'getoptions', ['sequential' => 1, 'field' => $field]);
      if ($result['is_error']) {
        throw new \Exception("Failed to getoptions for $entity.$field");
      }
      foreach ($result['values'] as $result) {
        if ($result['value'] == $label) {
          return $result['key'];
        }
      }
      throw new \Exception("Supplied value, '$label' is not a valid option.");
  }
  
  ```
  
  So then you can go:
  
  ```
  $gender_id = getIdForLabel('contact', 'gender_id', 'Male');
  civicrm_api3('contact', 'create', ['id'=>$contact_id, 'gender_id'=>$gender_id]);
  
  ```
  
  Clearly [username_3](https://civicrm.stackexchange.com/a/4351/35)'s answer is simpler :-)
  
  
  ---
  
  