Answers.Body: <p>The Contact.get API will return custom fields if requested:</p>
  
  <pre><code>&lt;?php
  
  $contacts = civicrm_api3('Contact', 'get', array(
    'sequential' =&gt; 1,
    'return' =&gt; array('custom_32'),
    'options' =&gt; array('limit' =&gt; 0),
  ));
  
  foreach ($contacts['values'] as $contact) {
    $res = civicrm_api3('Contact', 'create', array(
      'id' =&gt; $contact['id'],  
      'external_identifier' =&gt; $contact['custom_32'],
    ));
  }
  </code></pre>
  
  <p>You might want more parameters on the Contact.get (eg 'contact_is_deleted' => 0) and some error checking on the Contact.create.  As noted in previous comments, there are restrictions around external id's which the custom field may not comply with.</p>
  
  <p>If there are many contacts, you may need to change 'limit' and do it in chunks.</p>
  
  <p>You can save the above to a file, and use <a href="https://github.com/civicrm/cv" rel="nofollow noreferrer">cv</a> to run it:</p>
  
  <pre><code>cv scr /path/to/file
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-01-24T10:29:07.797Z
Answers.LastActivityDate: 2017-01-24T10:29:07.797Z
Answers.UserDisplayName: Aidan
Answers.Body: <p>so one way with the API
  lets assume your custom field is custom_32</p>
  
  <pre><code>$contacts = civicrm_api3('Contact', 'get', array(
        'options' =&gt; array('limit' =&gt; 9999),
      ));
  if($contacts['count']) {
    foreach($contacts['values'] as $contact_id =&gt; $value) {
  
          $result = civicrm_api3('CustomValue', 'get', array(
            'entity_id' =&gt; $contact_id,
          ));
          if(!$result['is_error'] &amp;&amp; isset($result['values'][32]['latest']) {
            $custom_field_value = $result['values'][32]['latest'];
  
            $contact_result = civicrm_api3('Contact', 'create', array(
              'id' =&gt; $contact_id,
              'external_identifier' =&gt; $custom_field_value,
            ));
          }
  }
  }
  </code></pre>
  
  <p>Now this is not very efficient if you have a large number of contacts. SQL would be better and I'm sure someone will come along with query for you.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-01-23T17:30:30.197Z
Answers.Text: if you running this in a Drupal module, or in Devel's execute php page, you will need to add a call to civicrm_initialize() before you run the api calls
Answers.UserDisplayName: jackrabbithanna
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-01-23T17:31:18.227Z
Answers.Text: also, the external identifier needs to be unique, so you may have to take that into account if you do this in mass for a bunch of contacts....
Answers.UserDisplayName: jackrabbithanna
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-01-23T19:11:28.967Z
Answers.Text: Is there a way to go through every record and apply this change? How can I find the list of contact_id
Answers.UserDisplayName: carclubadmin
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-01-23T20:15:40.090Z
Answers.Text: updated the answer with some logic, although if you have large number of contacts best to do direct SQL query
Answers.UserDisplayName: jackrabbithanna
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-01-26T16:55:00.013Z
Answers.Text: I've learned more about Civicrm in these two examples than I have on my own in months. Much appreciated. Thanks. Unfortunately, neither worked. I have to admit, I was surprised. I ran both through cv and I included the civicrm_initialize(). No changes at all. Is that weird or is something missing?
Answers.UserDisplayName: carclubadmin
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-01-23T17:28:28.300Z
Answers.LastActivityDate: 2017-01-23T20:14:49.080Z
Answers.LastEditDate: 2017-01-23T20:14:49.080Z
Answers.UserDisplayName: jackrabbithanna
Body: <p>Running Civi 4.7.13 with Drupal 7.52. I want to set the value of external_id in the Contacts table to be the same value as a custom field (using the API explorer, it seems this field is called custom_32). When I try to set the value from the SQL command line, nothing happens. I'm new with SQL and have little experience. I've tried the following. What do I need to change to get it to work? Is there an example I can follow (using the SQL command line or even API explorer)</p>
  
  <pre><code>SELECT civicrm
  UPDATE civicrm_contact c
  SET c.external_identifier = c.custom_32
  WHERE c.custom_32 is not null
  </code></pre>
  
  <p>It appears as though custom_32 is not part of the Contact table.</p>
  
Tags: <civimember><mysql>
Title: How do I set the value of external_id to be the same as a custom field?
ThreadText: # How do I set the value of external_id to be the same as a custom field?
  
  (Asked by: username_0 on 2017-01-23)
  
  Running Civi 4.7.13 with Drupal 7.52. I want to set the value of external\_id in the Contacts table to be the same value as a custom field (using the API explorer, it seems this field is called custom\_32). When I try to set the value from the SQL command line, nothing happens. I'm new with SQL and have little experience. I've tried the following. What do I need to change to get it to work? Is there an example I can follow (using the SQL command line or even API explorer)
  
  ```
  SELECT civicrm
  UPDATE civicrm_contact c
  SET c.external_identifier = c.custom_32
  WHERE c.custom_32 is not null
  
  ```
  
  It appears as though custom\_32 is not part of the Contact table.
  
  **Tags:** civimember, mysql
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2017-01-24. Score: 1
  
  The Contact.get API will return custom fields if requested:
  
  ```
  <?php
  
  $contacts = civicrm_api3('Contact', 'get', array(
    'sequential' => 1,
    'return' => array('custom_32'),
    'options' => array('limit' => 0),
  ));
  
  foreach ($contacts['values'] as $contact) {
    $res = civicrm_api3('Contact', 'create', array(
      'id' => $contact['id'],  
      'external_identifier' => $contact['custom_32'],
    ));
  }
  
  ```
  
  You might want more parameters on the Contact.get (eg 'contact\_is\_deleted' => 0) and some error checking on the Contact.create. As noted in previous comments, there are restrictions around external id's which the custom field may not comply with.
  
  If there are many contacts, you may need to change 'limit' and do it in chunks.
  
  You can save the above to a file, and use [cv](https://github.com/civicrm/cv) to run it:
  
  ```
  cv scr /path/to/file
  
  ```
  
  
  ---
  
  ### Answer by username_2 on 2017-01-23. Score: 0
  
  so one way with the API
  lets assume your custom field is custom\_32
  
  ```
  $contacts = civicrm_api3('Contact', 'get', array(
        'options' => array('limit' => 9999),
      ));
  if($contacts['count']) {
    foreach($contacts['values'] as $contact_id => $value) {
  
          $result = civicrm_api3('CustomValue', 'get', array(
            'entity_id' => $contact_id,
          ));
          if(!$result['is_error'] && isset($result['values'][32]['latest']) {
            $custom_field_value = $result['values'][32]['latest'];
  
            $contact_result = civicrm_api3('Contact', 'create', array(
              'id' => $contact_id,
              'external_identifier' => $custom_field_value,
            ));
          }
  }
  }
  
  ```
  
  Now this is not very efficient if you have a large number of contacts. SQL would be better and I'm sure someone will come along with query for you.
  
  > **Comments:**
  > **username_2**: if you running this in a Drupal module, or in Devel's execute php page, you will need to add a call to civicrm_initialize() before you run the api calls  
  > **username_2**: also, the external identifier needs to be unique, so you may have to take that into account if you do this in mass for a bunch of contacts....  
  > **username_0**: Is there a way to go through every record and apply this change? How can I find the list of contact_id  
  > **username_2**: updated the answer with some logic, although if you have large number of contacts best to do direct SQL query  
  > **username_0**: I've learned more about Civicrm in these two examples than I have on my own in months. Much appreciated. Thanks. Unfortunately, neither worked. I have to admit, I was surprised. I ran both through cv and I included the civicrm_initialize(). No changes at all. Is that weird or is something missing?  
  
  ---
  
  