Answers.Body: <p>I think you can use chain api to do this. Try using below snippet</p>
  
  <pre><code>$result = civicrm_api3('Contact', 'get', [
    'sequential' =&gt; 1,
    'return' =&gt; ["current_employer_id"],
    'custom_1' =&gt; ['IS NULL' =&gt; 1],
    'employer_id' =&gt; ['IS NOT NULL' =&gt; 1],
    'api.Contact.create' =&gt; [
      'id' =&gt; "\$value.id", 
      'custom_1' =&gt; "\$value.current_employer_id"
    ],
  ]);
  </code></pre>
  
  <p>(Replace custom_1 with custom field id from your system.)</p>
  
  <p>HTH</p>
  
  <p>Pradeep</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-23T17:38:41.733Z
Answers.LastActivityDate: 2018-09-23T17:38:41.733Z
Answers.UserDisplayName: Pradeep Nayak
Body: <p>Been playing with the API and see how to query for a result set but not the appropriate way to get a result set and update it batch style(over 4000 contacts in this case). </p>
  
  <p>My goal is to gather all contacts with an empty custom field value and copy the text value for 'current employer' into that field.</p>
  
  <p>This is in php using the 'class.api.php'. If there is a more appropriate way to achieve this I am open to suggestions.</p>
  
Tags: <api>
Title: Use the API to loop through contacts and copy field value
ThreadText: # Use the API to loop through contacts and copy field value
  
  (Asked by: username_0 on 2018-09-23)
  
  Been playing with the API and see how to query for a result set but not the appropriate way to get a result set and update it batch style(over 4000 contacts in this case).
  
  My goal is to gather all contacts with an empty custom field value and copy the text value for 'current employer' into that field.
  
  This is in php using the 'class.api.php'. If there is a more appropriate way to achieve this I am open to suggestions.
  
  **Tags:** api
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-09-23. Score: 1
  
  I think you can use chain api to do this. Try using below snippet
  
  ```
  $result = civicrm_api3('Contact', 'get', [
    'sequential' => 1,
    'return' => ["current_employer_id"],
    'custom_1' => ['IS NULL' => 1],
    'employer_id' => ['IS NOT NULL' => 1],
    'api.Contact.create' => [
      'id' => "\$value.id", 
      'custom_1' => "\$value.current_employer_id"
    ],
  ]);
  
  ```
  
  (Replace custom\_1 with custom field id from your system.)
  
  HTH
  
  username_1
  
  
  ---
  
  