Answers.Body: <p>Chain API call would be an answer for this. Let's for example :</p>
  
  <pre><code>$result = civicrm_api3('Country', 'get', array(
    'sequential' =&gt; 1,
     'name' =&gt; "India",
     'api.Contact.create' =&gt; array(
     'contact_id' =&gt; 202,
     'custom_7' =&gt; "\$value.id"),
  ));
  </code></pre>
  
  <p>So here we first fetching country on basis of its <code>'name' =&gt; "India"</code> and providing your desired custom field(related to entity Contact) with its country.id (here <code>"\$value.id"</code>)</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-31T17:22:38.127Z
Answers.Text: This is very interesting! Won't get to trying it for couple of weeks now, but thanks! My explorer doesn't list Country as an entity from memory. Also at a glance wouldn't you need to call getsingle on the outer api call?
Answers.UserDisplayName: artfulrobot
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-31T17:29:13.357Z
Answers.Text: That would work too (like an improvement) :)
Answers.UserDisplayName: Monish Deb
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-20T16:29:07.010Z
Answers.Text: Ah, that (API for Country) wasn't implemented in until after 4.4. But Chaining looks very handy, so thanks for your answer.
Answers.UserDisplayName: artfulrobot
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-31T17:15:48.070Z
Answers.LastActivityDate: 2015-07-31T17:15:48.070Z
Answers.UserDisplayName: Monish Deb
Answers.Body: <p>Any field (including custom fields) that accepts an option value will take either its machine name or it's id. The machine names for countries are their ISO codes, e.g. the code for 'United States' is 'US'. So you could do:</p>
  
  <pre><code>civicrm_api3('Contact', 'create', array(
    'custom_77' =&gt; 'US',
  ));
  </code></pre>
  
  <p>which is equivalent to</p>
  
  <pre><code>civicrm_api3('Contact', 'create', array(
    'custom_77' =&gt; 1228,
  ));
  </code></pre>
  
  <p>The label "Uganda" will not work, as it's not considered reliable given translations and word-replacements.</p>
  
  <p>To find the ISO code for a country, use the api explorer. Api <code>Country</code> <code>get</code> with <code>name = Uganda</code> reveals <code>"iso_code": "UG"</code></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-31T17:27:58.850Z
Answers.LastActivityDate: 2015-07-31T20:36:18.917Z
Answers.LastEditDate: 2015-07-31T20:36:18.917Z
Answers.UserDisplayName: Coleman
Body: <p>Some fields that are stored as id references to other tables can be set by simply passing the value as an id, e.g. <a href="https://civicrm.stackexchange.com/questions/4350/how-to-set-a-contacts-gender-using-api">setting gender_id=Male works</a>.</p>
  
  <p>I have a custom fieldset attached to contacts that includes a country (of origin - it's different to of residence!) field, known affectionately as <code>custom_77</code>.</p>
  
  <p>But I've noticed that the API doesn't like me to pass <code>custom_77=Uganda</code> - it complains that Uganda is not an integer. Can't fault it there :-)</p>
  
  <p>My <a href="https://civicrm.stackexchange.com/a/4353/35">answer to the setting gender question</a> provides a working solution: you can get it from <code>getIdForLabel('contact', 'custom_77', 'Uganda')</code>, or from any other country field, e.g. <code>getIdForLabel('address', 'country_id', 'Uganda')</code>, but as that turned out to be a very long winded solution when it was actually very simple, I thought I might, once again, be wrong! Hence this question.</p>
  
Tags: <api><custom-fields><lts>
Title: How to set a custom country field using the API
ThreadText: # How to set a custom country field using the API
  
  (Asked by: username_0 on 2015-07-31)
  
  Some fields that are stored as id references to other tables can be set by simply passing the value as an id, e.g. [setting gender\_id=Male works](https://civicrm.stackexchange.com/questions/4350/how-to-set-a-contacts-gender-using-api).
  
  I have a custom fieldset attached to contacts that includes a country (of origin - it's different to of residence!) field, known affectionately as `custom_77`.
  
  But I've noticed that the API doesn't like me to pass `custom_77=Uganda` - it complains that Uganda is not an integer. Can't fault it there :-)
  
  My [answer to the setting gender question](https://civicrm.stackexchange.com/a/4353/35) provides a working solution: you can get it from `getIdForLabel('contact', 'custom_77', 'Uganda')`, or from any other country field, e.g. `getIdForLabel('address', 'country_id', 'Uganda')`, but as that turned out to be a very long winded solution when it was actually very simple, I thought I might, once again, be wrong! Hence this question.
  
  **Tags:** api, custom-fields, lts
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2015-07-31. Score: 4 | Accepted answer
  
  Any field (including custom fields) that accepts an option value will take either its machine name or it's id. The machine names for countries are their ISO codes, e.g. the code for 'United States' is 'US'. So you could do:
  civicrm_api3('Contact', 'create', array(
    'custom_77' => 'US',
  ));
  
  which is equivalent to
  civicrm_api3('Contact', 'create', array(
    'custom_77' => 1228,
  ));
  
  The label "Uganda" will not work, as it's not considered reliable given translations and word-replacements.
  To find the ISO code for a country, use the api explorer. Api Country get with name = Uganda reveals "iso_code": "UG"
  
  
  ---
  
  ### Answer by username_1 on 2015-07-31. Score: 1
  
  Chain API call would be an answer for this. Let's for example :
  $result = civicrm_api3('Country', 'get', array(
    'sequential' => 1,
     'name' => "India",
     'api.Contact.create' => array(
     'contact_id' => 202,
     'custom_7' => "\$value.id"),
  ));
  
  So here we first fetching country on basis of its 'name' => "India" and providing your desired custom field(related to entity Contact) with its country.id (here "\$value.id")
  
  > **Comments:**
  > **username_0**: This is very interesting! Won't get to trying it for couple of weeks now, but thanks! My explorer doesn't list Country as an entity from memory. Also at a glance wouldn't you need to call getsingle on the outer api call?  
  > **username_1**: That would work too (like an improvement) :)  
  > **username_0**: Ah, that (API for Country) wasn't implemented in until after 4.4. But Chaining looks very handy, so thanks for your answer.  
  
  ---
  
  