Answers.Body: <p>I have expanded upon samuelsov's answer and created a new API function resembling CustomValue.create. It's not as general as the real create/setvalue APIs but it looks like it solves my immediate problem.</p>
  
  <p><a href="https://gist.github.com/futurefirst/9a6b39ddfa34eb01a417" rel="nofollow">https://gist.github.com/futurefirst/9a6b39ddfa34eb01a417</a></p>
  
  <p>Output:</p>
  
  <pre><code>$ drush cvapi CustomValue.insert entity_id=154621 custom_368=thisisatesthello
  Array
  (
      [is_error] =&gt; 0
      [version] =&gt; 3
      [count] =&gt; 1
      [id] =&gt; 23
      [values] =&gt; Array
          (
              [23] =&gt; Array
                  (
                      [id] =&gt; 23
                      [entity_id] =&gt; 154621
                      [custom_368] =&gt; thisisatesthello
                      [custom_368_23] =&gt; thisisatesthello
                  )
  
          )
  
  )
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-20T11:52:16.270Z
Answers.LastActivityDate: 2015-05-20T11:52:16.270Z
Answers.UserDisplayName: DaveFF
Answers.Body: <p>I don't think it's possible using the api right now. </p>
  
  <p>So what you could do in the mean time is create a custom function to do it and ideally emulate the api call using the same input params and output so you can replace it with an api call when the api is fixed.</p>
  
  <p>Not tested but should look like :</p>
  
  <pre><code>function insertCustomValue($params) {
  
    // prepare input params
    $dummy = explode('_', $params['field']);
    $cfId = $dummy[1];
    $cfValue = $params['value'];
    $entityId = $params['entity_id'];
  
    // get table name and column name
    $cf = civicrm_api3('CustomField', 'getsingle', array('id' =&gt; $cfId));
    $table_name = civicrm_api3('CustomGroup', 'getvalue', array(
      'id' =&gt; $cf['custom_group_id'], 
      'return' =&gt; 'table_name')
    );
    $column_name = $cf['column_name'];
  
    // insert the new value in the database
    $sql = "INSERT INTO $table_name (entity_id, $column_name) values (%1, %2)";
    $dao = CRM_Core_DAO::executeQuery($sql, array(
      1 =&gt; array($entityId, 'Integer'), 
      2 =&gt; array($cfValue, 'String')
    ));
  
    // get and return the id of the new row
    $cfId = CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()');
    return array(
      'is_error' =&gt; 0,
      'count' =&gt; 1,
      'values' =&gt; array(
        'id' =&gt; $cfId,
      )
    );
  
  }
  </code></pre>
  
  <p>But of course you could also help fix the api which would be better :)</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-15T16:54:10.950Z
Answers.LastActivityDate: 2015-05-15T16:54:10.950Z
Answers.UserDisplayName: samuelsov
Body: <p>The API calls I know of that add a record to a multi-value custom field, don't return the ID of the added record (which you can then use to access the value for the specific record in the form custom_XXX_YY). Is there another way of finding it?</p>
  
  <p><strong>Observed:</strong></p>
  
  <pre><code>$ drush cvapi CustomValue.create entity_id=154621 custom_368=Test                                                                                                                                      
  Array
  (
      [is_error] =&gt; 0
      [version] =&gt; 3
      [count] =&gt; 1
      [values] =&gt; 1
  )
  
  $ drush cvapi Contact.setvalue id=154621 field=custom_368 value=test                                                                                                                                   
  Array
  (
      [is_error] =&gt; 0
      [version] =&gt; 3
      [count] =&gt; 2
      [values] =&gt; Array
          (
              [id] =&gt; 154621
              [custom_368] =&gt; test
          )
  
  )
  
  $ drush cvapi Contact.create custom_368= id=154621                                                                                                                                                     
  Array
  (
      [is_error] =&gt; 0
      [version] =&gt; 3
      [count] =&gt; 1
      [id] =&gt; 154621
      [values] =&gt; Array
          (
              [154621] =&gt; Array
                  (
                      [id] =&gt; 154621
                      [contact_type] =&gt; Individual
                      ... snipped ...
                      [modified_date] =&gt; 2015-05-14 11:20:56
                  )
  
          )
  
  )
  </code></pre>
  
  <p><strong>Background:</strong></p>
  
  <p>In our custom frontend I want to have an 'Add new qualification' link, which creates a record in the appropriate custom field group and then adds the set of fields to the form. The screen size occupied by a set of fields for one qualification is such that I don't want to always have a blank set of fields on the screen for adding a new record, before I've clicked the link. The record's row ID is necessary to add the form fields so they know which record they apply to. Each field is 'inline editable' / sends an update call immediately.</p>
  
Tags: <api><custom-fields>
Title: How to get the new record ID, when adding a record to a multi-value custom field using the API?
ThreadText: # How to get the new record ID, when adding a record to a multi-value custom field using the API?
  
  (Asked by: username_0 on 2015-05-15)
  
  The API calls I know of that add a record to a multi-value custom field, don't return the ID of the added record (which you can then use to access the value for the specific record in the form custom\_XXX\_YY). Is there another way of finding it?
  
  **Observed:**
  
  ```
  $ drush cvapi CustomValue.create entity_id=154621 custom_368=Test                                                                                                                                      
  Array
  (
      [is_error] => 0
      [version] => 3
      [count] => 1
      [values] => 1
  )
  
  $ drush cvapi Contact.setvalue id=154621 field=custom_368 value=test                                                                                                                                   
  Array
  (
      [is_error] => 0
      [version] => 3
      [count] => 2
      [values] => Array
          (
              [id] => 154621
              [custom_368] => test
          )
  
  )
  
  $ drush cvapi Contact.create custom_368= id=154621                                                                                                                                                     
  Array
  (
      [is_error] => 0
      [version] => 3
      [count] => 1
      [id] => 154621
      [values] => Array
          (
              [154621] => Array
                  (
                      [id] => 154621
                      [contact_type] => Individual
                      ... snipped ...
                      [modified_date] => 2015-05-14 11:20:56
                  )
  
          )
  
  )
  
  ```
  
  **Background:**
  
  In our custom frontend I want to have an 'Add new qualification' link, which creates a record in the appropriate custom field group and then adds the set of fields to the form. The screen size occupied by a set of fields for one qualification is such that I don't want to always have a blank set of fields on the screen for adding a new record, before I've clicked the link. The record's row ID is necessary to add the form fields so they know which record they apply to. Each field is 'inline editable' / sends an update call immediately.
  
  **Tags:** api, custom-fields
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2015-05-15. Score: 4 | Accepted answer
  
  I don't think it's possible using the api right now.
  
  So what you could do in the mean time is create a custom function to do it and ideally emulate the api call using the same input params and output so you can replace it with an api call when the api is fixed.
  
  Not tested but should look like :
  
  ```
  function insertCustomValue($params) {
  
    // prepare input params
    $dummy = explode('_', $params['field']);
    $cfId = $dummy[1];
    $cfValue = $params['value'];
    $entityId = $params['entity_id'];
  
    // get table name and column name
    $cf = civicrm_api3('CustomField', 'getsingle', array('id' => $cfId));
    $table_name = civicrm_api3('CustomGroup', 'getvalue', array(
      'id' => $cf['custom_group_id'], 
      'return' => 'table_name')
    );
    $column_name = $cf['column_name'];
  
    // insert the new value in the database
    $sql = "INSERT INTO $table_name (entity_id, $column_name) values (%1, %2)";
    $dao = CRM_Core_DAO::executeQuery($sql, array(
      1 => array($entityId, 'Integer'), 
      2 => array($cfValue, 'String')
    ));
  
    // get and return the id of the new row
    $cfId = CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()');
    return array(
      'is_error' => 0,
      'count' => 1,
      'values' => array(
        'id' => $cfId,
      )
    );
  
  }
  
  ```
  
  But of course you could also help fix the api which would be better :)
  
  
  ---
  
  ### Answer by username_0 on 2015-05-20. Score: 2
  
  I have expanded upon username_1's answer and created a new API function resembling CustomValue.create. It's not as general as the real create/setvalue APIs but it looks like it solves my immediate problem.
  
  <https://gist.github.com/futurefirst/9a6b39ddfa34eb01a417>
  
  Output:
  
  ```
  $ drush cvapi CustomValue.insert entity_id=154621 custom_368=thisisatesthello
  Array
  (
      [is_error] => 0
      [version] => 3
      [count] => 1
      [id] => 23
      [values] => Array
          (
              [23] => Array
                  (
                      [id] => 23
                      [entity_id] => 154621
                      [custom_368] => thisisatesthello
                      [custom_368_23] => thisisatesthello
                  )
  
          )
  
  )
  
  ```
  
  
  ---
  
  