For doing updates to multiple contacts using the API, is there a better way than this?

$res = civicrm_api3('Contact', 'get', $params);
foreach ($res['values'] as $contact) {
  civicrm_api3('Contact', 'create', array('id' => $contact['id'], ... ))

For a large number of contacts, this is slow and eventually runs out of memory.

1 Answer 1


You can use chaining:

civicrm_api3('Contact', 'get', array(
  'return' => 'id', // more efficient since you don't care about the get results
  ... other search params ...
  'api.Contact.create' => array('last_name' => 'Something'),

The 'id' will be set for you automatically by the chaining. For more about chaining see this post.

Also remember the 'gotcha' that unless you specify otherwise, this will only fetch & update 25 records.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.