2

I recently have taken over managing CiviCRM for an orchestra in Holland. Some fields have been used inconsistently, for example Nickname and First Name. I now need to copy all data in Nickname to First Name. That would be no problem running a SQL query, but I am afraid that would be too bold a step in CiviCRM. How do I go about? Is there an extension to do the job?

2
  • 1
    Why not just import the nickname field to first name field?
    – Andy Burns
    Jan 12 at 4:16
  • 1
    this is something we would likely do via an sql query - but you would also need to then update Display Name and Sort Name fields, so they showed the updated data, via another query
    – petednz - fuzion
    Jan 13 at 2:43
1

I don't think there's an extension but there is an api (https://docs.civicrm.org/dev/en/latest/api/). If you do it in SQL it wouldn't autoupdate other items like the "display_name", so you could write a little php script using the api to do it, and then run it with cv php:script. Basically you would want Contact.get and then Contact.create, e.g. something like (untested! Make a backup!)

$contacts = civicrm_api3('contact', 'get', ['contact_type' => 'Individual']);
foreach ($contacts['values'] as $c) {
  civicrm_api3('contact', 'create', [
    'id' => $c['id'],
    'first_name' => $c['nick_name'],
  ]);
}

You may also want to e.g. check that nick_name isn't blank for that record. May need to experiment.

FYI contact.create means "update" when you give it an id.

2
  • Great, thanks, I am going to work on it! Jan 11 at 20:02
  • 1
    another option might be an export/import but would need to check how to deal with a field, eg nickname, which might have data but which should be overwritten to be NULL or empty
    – petednz - fuzion
    Jan 13 at 2:42
2

You could do it just with API Explorer 4 using chaining:

enter image description here

I've added a couple of 'where' clauses for minor sanity - you might want to adjust those.

This finds all the contacts satisfying the 'where' clauses and for each one, runs an update where $id and $nick_name in the chain clause refer to the values from the contact just looked up.

Take a backup & test carefully!

1
  • Thanks for the great write up on how to do this
    – petednz - fuzion
    Jan 14 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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