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?
-
1Why not just import the nickname field to first name field?– Andy Burns - iXiamJan 12, 2022 at 4:16
-
1this 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, 2022 at 2:43
2 Answers
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
.
-
-
1another 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, 2022 at 2:42
You could do it just with API Explorer 4 using chaining:
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!