Answers.Body: <p>As of CiviCRM 4.6, the <a href="https://civicrm.org/blogs/colemanw/meet-new-api-explorer" rel="noreferrer">api explorer</a> is like 1-stop-shopping for browsing, building and testing api calls.</p>
  
  <ul>
  <li><p>You can start on the "Examples" tab to see how custom values are generally done:
  <img src="https://i.sstatic.net/Bly2S.png" alt="Api Examples"></p></li>
  <li><p>If you want more detailed explanation of how it works, try the "Code Docs" tab:
  <img src="https://i.sstatic.net/xOabs.png" alt="Api Docs"></p></li>
  <li><p>And finally, the Explorer tab lets you browse available params and will write the php/javascript/rest/cli code for you:
  <img src="https://i.sstatic.net/0trAV.png" alt="Api Explorer"></p></li>
  </ul>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-26T20:03:55.470Z
Answers.LastActivityDate: 2015-03-26T20:03:55.470Z
Answers.UserDisplayName: Coleman
Answers.Body: <p>Note that 'update' action is deprecated and replaced by 'create'. If you give the primary fields in the params (usually id but it's entity_id+custom_xx in your case), you will automatically get an update.</p>
  
  <p>Also note that there are examples of api uses in <a href="https://github.com/civicrm/civicrm-core/tree/master/api/v3/examples" rel="nofollow">https://github.com/civicrm/civicrm-core/tree/master/api/v3/examples</a>. It's very helpful to see what are the possibilities and what are the results we can expect.</p>
  
  <p>Last, you could use civicrm_api3 instead of civicrm_api so you don't have to define the version in the params, which i personnaly found awkward.</p>
  
  <p>Having that say, I guess the best way to do what you want would be :</p>
  
  <pre><code>$params = array(
    'custom_160' =&gt; $badges,
    'entity_id' =&gt; $contact_id,
  );
  try {
    $result = civicrm_api3('CustomValue', 'create', $params);
  }
  catch (CiviCRM_API3_Exception $e) {
    // Handle error here if you want
  );
  </code></pre>
  
  <p>If you are pretty sure of what you are doing and want a concise way, you could do :</p>
  
  <pre><code>$params = array('custom_160' =&gt; $badges, 'entity_id' =&gt; $contact_id);
  $result = civicrm_api3('CustomValue', 'create', $params);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-30T13:23:51.873Z
Answers.LastActivityDate: 2015-03-30T13:29:07.023Z
Answers.LastEditDate: 2015-03-30T13:29:07.023Z
Answers.UserDisplayName: samuelsov
Answers.Body: <p>My first stopping points on how to work with CiviCRM API are usually -</p>
  
  <ul>
  <li><a href="https://github.com/civicrm/civicrm-core/blob/master/api/v3/examples" rel="nofollow">API Examples</a></li>
  <li><a href="https://github.com/civicrm/civicrm-core/blob/master/tests/phpunit/api/v3/" rel="nofollow">API Tests</a></li>
  </ul>
  
  <p>In those directories you'll find examples for <em>most</em> API usage.</p>
  
  <pre><code>$params = array(
    'first_name' =&gt; 'abc3',
    'last_name' =&gt; 'xyz3',
    'contact_type' =&gt; 'Individual',
    'email' =&gt; 'foo@example.com',
    'custom_123' =&gt; 'foo',
  );
  try {
    $result = civicrm_api3('CustomValue', 'create', $params);
  }
  catch (Exception $e) {
    // uhoh
  }
  </code></pre>
  
  <p>Looking up custom field IDs in API calls can require a bit of additional code beforehand.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-26T18:51:11.700Z
Answers.LastActivityDate: 2015-03-26T19:46:44.797Z
Answers.LastEditDate: 2015-03-26T19:46:44.797Z
Answers.UserDisplayName: Chris Burgess
Answers.Body: <p>If you want to get the CustomValue of other entities than Contact, you need to specify the entity_table. I think it could be good to have this in the api examples because it is indeed not there and very hard to find this info. </p>
  
  <p>Example:</p>
  
  <pre><code>$result = civicrm_api3('CustomValue', 'get', array(
    'sequential' =&gt; 1,
    'entity_table' =&gt; "Participant",
    'entity_id' =&gt; 23,
  ));
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-10T08:26:23.010Z
Answers.LastActivityDate: 2016-08-10T08:26:23.010Z
Answers.UserDisplayName: Hans Rossel
Body: <p>I always seem to struggle to find a concise way of working with custom data via the API. I know that something like...</p>
  
  <pre><code>$updateParams = array('version' =&gt; 3, 'contact_id' =&gt; $contact_id, 'custom_160' =&gt; $badges);
  $result=civicrm_api('contact', 'update', $updateParams);
  </code></pre>
  
  <p>...works for some but not all entities, so I presume that I'm meant to be using the CustomValue API, but whenever I do, my code looks v. clunky / doesn't work / I resort to SQL in frustration.  If anyone has a concise example or two of CustomValue in use that would be most appreciated.</p>
  
Tags: <api><custom-fields>
Title: What is the correct way to work with custom data in the API?
ThreadText: # What is the correct way to work with custom data in the API?
  
  (Asked by: username_0 on 2015-03-25)
  
  I always seem to struggle to find a concise way of working with custom data via the API. I know that something like...
  
  ```
  $updateParams = array('version' => 3, 'contact_id' => $contact_id, 'custom_160' => $badges);
  $result=civicrm_api('contact', 'update', $updateParams);
  
  ```
  
  ...works for some but not all entities, so I presume that I'm meant to be using the CustomValue API, but whenever I do, my code looks v. clunky / doesn't work / I resort to SQL in frustration. If anyone has a concise example or two of CustomValue in use that would be most appreciated.
  
  **Tags:** api, custom-fields
  
  
  ---
  
  ## 4 answers
  
  ### Answer by username_1 on 2015-03-26. Score: 8
  
  As of CiviCRM 4.6, the [api explorer](https://civicrm.org/blogs/colemanw/meet-new-api-explorer) is like 1-stop-shopping for browsing, building and testing api calls.
  
  * You can start on the "Examples" tab to see how custom values are generally done:
    ![Api Examples](https://i.sstatic.net/Bly2S.png)
  * If you want more detailed explanation of how it works, try the "Code Docs" tab:
    ![Api Docs](https://i.sstatic.net/xOabs.png)
  * And finally, the Explorer tab lets you browse available params and will write the php/javascript/rest/cli code for you:
    ![Api Explorer](https://i.sstatic.net/0trAV.png)
  
  
  ---
  
  ### Answer by username_2 on 2015-03-30. Score: 2
  
  Note that 'update' action is deprecated and replaced by 'create'. If you give the primary fields in the params (usually id but it's entity\_id+custom\_xx in your case), you will automatically get an update.
  
  Also note that there are examples of api uses in <https://github.com/civicrm/civicrm-core/tree/master/api/v3/examples>. It's very helpful to see what are the possibilities and what are the results we can expect.
  
  Last, you could use civicrm\_api3 instead of civicrm\_api so you don't have to define the version in the params, which i personnaly found awkward.
  
  Having that say, I guess the best way to do what you want would be :
  
  ```
  $params = array(
    'custom_160' => $badges,
    'entity_id' => $contact_id,
  );
  try {
    $result = civicrm_api3('CustomValue', 'create', $params);
  }
  catch (CiviCRM_API3_Exception $e) {
    // Handle error here if you want
  );
  
  ```
  
  If you are pretty sure of what you are doing and want a concise way, you could do :
  
  ```
  $params = array('custom_160' => $badges, 'entity_id' => $contact_id);
  $result = civicrm_api3('CustomValue', 'create', $params);
  
  ```
  
  
  ---
  
  ### Answer by username_3 on 2015-03-26. Score: 1
  
  My first stopping points on how to work with CiviCRM API are usually -
  
  * [API Examples](https://github.com/civicrm/civicrm-core/blob/master/api/v3/examples)
  * [API Tests](https://github.com/civicrm/civicrm-core/blob/master/tests/phpunit/api/v3/)
  
  In those directories you'll find examples for *most* API usage.
  
  ```
  $params = array(
    'first_name' => 'abc3',
    'last_name' => 'xyz3',
    'contact_type' => 'Individual',
    'email' => 'foo@example.com',
    'custom_123' => 'foo',
  );
  try {
    $result = civicrm_api3('CustomValue', 'create', $params);
  }
  catch (Exception $e) {
    // uhoh
  }
  
  ```
  
  Looking up custom field IDs in API calls can require a bit of additional code beforehand.
  
  
  ---
  
  ### Answer by username_4 on 2016-08-10. Score: 0
  
  If you want to get the CustomValue of other entities than Contact, you need to specify the entity\_table. I think it could be good to have this in the api examples because it is indeed not there and very hard to find this info.
  
  Example:
  
  ```
  $result = civicrm_api3('CustomValue', 'get', array(
    'sequential' => 1,
    'entity_table' => "Participant",
    'entity_id' => 23,
  ));
  
  ```
  
  
  ---
  
  