CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • import CSV with Tags and Groups
Pages: [1]

Author Topic: import CSV with Tags and Groups  (Read 4382 times)

kimkennof

  • I’m new here
  • *
  • Posts: 9
  • Karma: 0
  • CiviCRM version: 4.1.3
  • CMS version: Drupal
  • MySQL version: 5
  • PHP version: 5.2
import CSV with Tags and Groups
October 10, 2012, 01:59:11 am
Hi guys,

I need to be able to import Contacts from a CSV into CiviCRM, but the process should also be able to read Tag-columns and/or Group-columns.

Ideally, we should be able to import the same file as we can export using "all possible fields" for a Contact:

Internal Contact ID;Contact Type;Contact Subtype;Do Not Email;Do Not Phone;Do Not Mail;Do Not Sms;Do Not Trade;No Bulk Emails (User Opt Out);Legal Identifier;External Identifier;Sort Name;Display Name;Nick Name;Legal Name;Image Url;Preferred Communication Method;Preferred Language;Preferred Mail Format;Contact Hash;Source of Contact Data;First Name;Middle Name;Last Name;Job Title;Birth Date;Is Deceased;Deceased Date;Household Name;Organisation Name;Sic Code;Unique ID (OpenID);Current Employer ID;Contact is in Trash;Addressee;Email Greeting;Postal Greeting;Gender;Individual Prefix;Individual Suffix;Current Employer;Location Type;Street Address;Street Number;Street Number Suffix;Street Name;Street Unit;Additional Address 1;Additional Address 2;City;Post Code Suffix;Postal Code;Geo Code 1;Geo Code 2;Address Name;Master Address Belongs To;County;State;Country;Phone;Phone Extension;Email;On Hold;Use for Bulk Mail;Signature Text;Signature Html;IM Provider;IM Screen Name;OpenID;World Region;Website;Group(s);Tag(s);Note(s);Phone Type;Im Service Provider

This way users can Export data, modify it in bulk in their favorite spreadsheet editor, and then re-import the data into CiviCRM.


Currently I'm able to map every possible Contact field using the field-mapper utility, except for Tags and Groups.

These two data-fields should be "imported" or better, selected on the "Preview" step, making it very difficult to set different tags/groups for different rows in the import file.

What I want to know is:

1. How can I add extra mapping fields?

2. What is the behaviour when setting multiple columns to the same mapped field? We want to be able to use for ex. Column #1, #7, #9 as Tags, and Column #4 as Group

3. Should I do this by patching / extending the existing importer, or create a duplicate version in a Drupal module, which can then be contributed back and Civi can decide if/how to incorporate it into Core?


xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: import CSV with Tags and Groups
October 10, 2012, 03:03:31 am
Hi,

Not sure about how to implement but would be super useful

One thing to take into account: how to deal with invalid values into the column, eg. a tag or group that doesn't exist? The normal behaviour is to skip the row, but might be better in this case to create the contact and not assign to the tag/group?

Something else that was suggested: being able to map a column to a specific tag/group, and assign the contact to the group/tag if the cell is not empty (eg. contains "X")

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

kimkennof

  • I’m new here
  • *
  • Posts: 9
  • Karma: 0
  • CiviCRM version: 4.1.3
  • CMS version: Drupal
  • MySQL version: 5
  • PHP version: 5.2
Re: import CSV with Tags and Groups
October 10, 2012, 03:20:07 am
Hi Xavier,

1. When processing the rows in the file (not really, I would use the database table created from the file), I would do the following:

-- Find all Tag/Group columns and aggregate values into a single Tag-Array / Group-Array
-- -- Note: one Tag / Group column can contain multiple items, using a "tag, tag, tag" / "group, group" format

-- Do a array_unique() on these Arrays to ensure unique tags, or a Contact might get a double reference to a Tag / Group

2. When our Arrays are cleaned up and contain only unique Tags / Groups I would loop through these Arrays and:

-- Find their ID in the database to see if Tag / Group exists
-- Find out if the found ID is already linked to the Contact by looking up the email address (or any other "unique" identifier field found in the mapping Array)

3. If Tag / Group not yet linked to our Contact:

-- Use the found ID and add it to our Contact

Each row in our CSV would at least require 3 SELECT queries (tag exists? contact exists? tag is linked to contact?) , which can trigger a performance issue on large imports.

A plausible solution to this issue might be to use the Drupal Batch API, which allows for large actions to be chopped up in bits.

This solution would then make this feature Drupal-dependant for large files so it's far from optimal.

I would like to have some feedback on the performance question :)

Greets,
Kim

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: import CSV with Tags and Groups
October 10, 2012, 06:31:45 am
If importing a column group (res. tag), that's probably worthwhile to load all the groups (tags) at the start of the import, swap the key and value so you get a table group['name of group A" => 28, "another group" => 42...

for each row you do a key_exists on that table

for the insert, not sure how it's done now in the bao, but you can do an insert on duplicate key update, would probably be more performant than select follow by insert (at least if the column is having only one or few tags)


X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

kimkennof

  • I’m new here
  • *
  • Posts: 9
  • Karma: 0
  • CiviCRM version: 4.1.3
  • CMS version: Drupal
  • MySQL version: 5
  • PHP version: 5.2
Re: import CSV with Tags and Groups
October 10, 2012, 06:46:48 am
Indeed xavier, it would be wise to preload ALL groups / tags into an Array with key = ID, value = Text to match.

adixon

  • I post frequently
  • ***
  • Posts: 314
  • Karma: 19
    • Blackfly Solutions
Re: import CSV with Tags and Groups
October 10, 2012, 06:52:47 am
I'm not convinced this is a use-case worth pursuing.

My experience with working with Excel and users is that the output is really hard to predict. Particularly date formatting, but it applies to a lots of fields. So your code for re-importing whatever your users do would be impossible to make bullet-proof, and you'd run the risk of mass data corruption all the time.

In other words - excel is familiar to a lot of users, so that makes it good, but it's really not the right tool for editing CiviCRM records (or frankly, any database records, imho ...), because it's a much more general purpose tool.

I think it would be more productive to work on web interface tools that do the kinds of things you would do in excel. It could even be a Drupal module!

There is a use of CiviCRM profiles that might already be the start of a project like this.

Re: importing tags and groups, yes that's an issue that I've had in migration from other systems, but it's solvable by using custom fields that can then be converted to tags and groups (manually, assuming there aren't too many of them).

kimkennof

  • I’m new here
  • *
  • Posts: 9
  • Karma: 0
  • CiviCRM version: 4.1.3
  • CMS version: Drupal
  • MySQL version: 5
  • PHP version: 5.2
Re: import CSV with Tags and Groups
October 10, 2012, 07:08:08 am
I appreciate your opinion adixon and share the same scepticism about Excel to CSV files, based on previous experiences with localisation, but I believe I can warrant this modification for the following reasons:

1. Currently one is able to search + export Contacts as a CSV file, using a mapping feature to specify the columns to export.

The primary fields in these exports already contain a Comma separated list of Tags and Groups associated with the Contact.


2. Currently one is able to import / update Contacts from a CSV (or SQL) file, using a mapping feature to specify which column represents which Contact field.

This feature lacks the possibility to map a column to Tags / Groups.


3. As you can export Contacts, one would believe it is quite easy to import that same file back into CiviCRM with the changed data. It is however NOT in a multilingual environment because of certain enum-type values being translated.


4. Some of our users aren't able to change data in the CRM and use the exported CSV files to update certain information, which is then sent back. It would require a substantial manual effort to preprocess those same CSV files before importing them.

Finally: Simply being able to export and import the same (but modified) file, would benefit a lot of users.

britebyte

  • I post occasionally
  • **
  • Posts: 70
  • Karma: 2
  • CiviCRM version: Several 3.4+, 4.1+
  • CMS version: Drupal 6, Drupal7
  • MySQL version: Several 5.1+
  • PHP version: Several 5.2+
Re: import CSV with Tags and Groups
October 24, 2012, 06:47:49 am
If you are OK with a Drupal only solution you might consider using the Migrate module which already uses the batch api and work with Eileen McNaughton's Civimigrate target as a base:
http://drupal.org/project/migrate and http://drupal.org/sandbox/eileen/1658172.

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: import CSV with Tags and Groups
November 30, 2012, 11:59:32 am
This feature is requested on a semi-regular basis, and kind of make sense.

I think a simple option (it only does something if the tag/group exists already, never creates it) would work.  Migration and the api works, but that's probably too close to the machine for "normal" usage, only for migrations.

If someone is willing to invest and contribute back, think it would be an improvement

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

coopersquare

  • I’m new here
  • *
  • Posts: 14
  • Karma: 0
    • Cooper Square Committee
  • CiviCRM version: 4.4.4
  • CMS version: Wordpress 3.8
  • MySQL version: 5.1
  • PHP version: 5.3.3
Re: import CSV with Tags and Groups
December 05, 2013, 01:20:55 pm
For adding Tag or Group designations to Contacts, one can import Contacts and choose in the last step before running the Import to assign Tags or Groups to them. I often need to Tag a bunch of Contacts, by ID number, and so I realized I can upload a CSV of just ID numbers, and then choose a Tag. (I wonder if there is an easier way to do this via an SQL command?)

Detlev

  • I post occasionally
  • **
  • Posts: 68
  • Karma: 2
    • Software für Engagierte e.V.
  • CiviCRM version: 4.5
  • CMS version: Drupal
Re: import CSV with Tags and Groups
December 06, 2013, 12:53:24 am
Quote from: coopersquare on December 05, 2013, 01:20:55 pm
For adding Tag or Group designations to Contacts, one can import Contacts and choose in the last step before running the Import to assign Tags or Groups to them. I often need to Tag a bunch of Contacts, by ID number, and so I realized I can upload a CSV of just ID numbers, and then choose a Tag. (I wonder if there is an easier way to do this via an SQL command?)

Yes, this works - for tags as well as for groups.

But, be careful: If you are doing an import using duplicate rules, only the newly inserted contacts will be tagged resp. put into the group!!

As a workaround, I prefer to import the group name (or tag name) into a custom field, and in a second step, I search for that custom field content, and add the contacts to the group via the "Actions" menu.


JohnFF

  • I post frequently
  • ***
  • Posts: 235
  • Karma: 6
  • CiviCRM version: 4.4.13
  • CMS version: Drupal 7.28
  • MySQL version: 5.5.31-1
  • PHP version: 5.3.27
Re: import CSV with Tags and Groups
December 12, 2013, 01:26:40 am
A couple of opinions/observances to share:

1) This is probably going to be a useful feature for advanced users.

2) Mass data corruption is a real worry. Excel hates your date format of choice and loves chopping the first 0 off phone numbers without spaces in etc. That wouldn't affect Contact editing, but worth bearing in mind.

3) How would you deal with instances where someone included a group that didn't already exist? Flag it as an error and not import? What if people wanted to use Excel to mass create groups in the first place?

4) If you're adding validation to the data import, please can you add a "validation hook" where I can add my own validation to import fields?

5) In the pre-process hook of the data upload, if it returns false and doesn't create the contact it will still tell you that it has created it! (v 4.2.13). I'm trying to get details on the use case so I can log a bug.
If you like empowering charities in a free and open way, then you're going to love Civi.

Email Amender: https://civicrm.org/extensions/email-amender
UK Phone Validator: https://civicrm.org/extensions/uk-phone-number-validator
http://civifirst.com
https://twitter.com/civifirst

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • import CSV with Tags and Groups

This forum was archived on 2017-11-26.