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) »
  • Support »
  • Using CiviCRM »
  • Using Import (Moderator: Yashodha Chaku) »
  • Importing to multiple choice fields
Pages: [1] 2

Author Topic: Importing to multiple choice fields  (Read 4883 times)

ericaordinary

  • Guest
Importing to multiple choice fields
June 30, 2010, 07:18:40 pm
Hi all,

I've had a search through the documentation and forum but can't seem to find an answer to this question.

When importing data into a custom Alphanumeric Autocomplete-Select field do you need to define the multiple choice options before you import, or will CiviCRM create the options based on the data you are importing?

If you do need to create the values in the CRM beforehand is there a way to import these with a script or similar?  I ask because I have a list of about 300 multiple choice options that need to be imported into a particular custom field and don't really fancy typing them all in manually.

Cheers,
Erica.

xcf33

  • I post frequently
  • ***
  • Posts: 181
  • Karma: 7
  • CiviCRM version: 3.3.2
  • CMS version: Drupal 6.19/6.20
  • MySQL version: 5.x
  • PHP version: 5.2.6
Re: Importing to multiple choice fields
June 30, 2010, 08:07:34 pm
Hi Erica,

I believe that there's field level import validation, which means:

If your custom field has the following selection in the option group (values)

1. apple
2. banana
3. orange
4. pine apple

Then when you import, you have to make sure that CSV column contain one of the 4 values or otherwise during the preview step CiviCRM will tell you that the value imported does not match the value defined from the custom field.


Hope it helps,


Cheers!

Rahul Bile

  • I post occasionally
  • **
  • Posts: 112
  • Karma: 16
  • impossible says, I M Possible
    • I AM POSSIBLE
Re: Importing to multiple choice fields
June 30, 2010, 09:36:04 pm
Erica,

Quote
When importing data into a custom Alphanumeric Autocomplete-Select field do you need to define the multiple choice options before you import
- Yes.

Quote
will CiviCRM create the options based on the data you are importing?
- No

As xcf33 said,  your csv should contain the values which exists in the options of that field.
Note : Even instead of value if the label  exists in your csv,  then it matches the correct value.

Rahul
Consider donating to CiviCRM if you use it. http://civicrm.org/donate

ericaordinary

  • Guest
Re: Importing to multiple choice fields
June 30, 2010, 09:43:59 pm
Thanks guys, that makes sense.

So is there any way to import the multiple choice options from a spreadsheet or should I fix myself a strong cup of coffee and start typing?

Yashodha Chaku

  • Forum Godess / God
  • Ask me questions
  • *****
  • Posts: 755
  • Karma: 57
    • CiviCRM
Re: Importing to multiple choice fields
June 30, 2010, 10:41:32 pm
If the custom data field can have multiple values (is of type multi-select, checkbox, etc) you could specify the comma separated list of options in your csv file provided the custom data options have already been created in the system.

-Yashodha
Found this reply helpful? Contribute NOW and help us improve CiviCRM with the Make it Happen! initiative.

ericaordinary

  • Guest
Re: Importing to multiple choice fields
June 30, 2010, 11:41:14 pm
Yashoda, the data field will only have one value (I'm using AutocompleteSelect type), but that one value will come from a list of over 300 possible values that need to be choices for this particular field.  If what you're saying is correct then this means I will have to manually enter all 300 values into CiviCRM before importing my data.

Is there a way of importing the 300 multiple choice options (perhaps using SQL?) into CiviCRM prior to importing my data (each record will have only one of the 300 possible values for this field) so that I don't have to manually sit and add 300 "new options" within CiviCRM?

xcf33

  • I post frequently
  • ***
  • Posts: 181
  • Karma: 7
  • CiviCRM version: 3.3.2
  • CMS version: Drupal 6.19/6.20
  • MySQL version: 5.x
  • PHP version: 5.2.6
Re: Importing to multiple choice fields
July 01, 2010, 05:56:45 am
Yes there is a way of doing that, you can use XML schema to load in the multiple choice as an option group and option values from a XML file with the proper schema. You can find more information on how to do that from CiviEngage.

But basically, here's the snippet

Code: [Select]
<?xml version="1.0" encoding="iso-8859-1" ?>
<CustomData>
<CustomGroups>
    <CustomGroup>
      <name>Primary_Contact</name>
      <title>Primary Contact</title>
      <extends>Individual</extends>
      <style>Inline</style>
      <collapse_display>0</collapse_display>
      <help_pre></help_pre>
      <help_post></help_post>
      <weight>1</weight>
      <is_active>1</is_active>
      <table_name>civicrm_value_primary_contact</table_name>
      <is_multiple>0</is_multiple>
      <collapse_adv_display>0</collapse_adv_display>
    </CustomGroup>
<CustomGroups>
 </CustomField>
    <CustomField>
      <label>Activity</label>
      <data_type>String</data_type>
      <html_type>Select</html_type>
      <is_required>0</is_required>
      <is_searchable>1</is_searchable>
      <is_search_range>0</is_search_range>
      <weight>30</weight>
      <is_active>1</is_active>
      <is_view>0</is_view>
      <text_length>255</text_length>
      <note_columns>60</note_columns>
      <note_rows>4</note_rows>
      <column_name>Activity Type</column_name>
      <option_group_name>activity_type</option_group_name>
      <custom_group_name>Primary_Contact</custom_group_name>
    </CustomField>
<OptionGroups>
<OptionGroup>
      <name>activity_type</name>
      <description>Activity Type</description>
      <is_reserved>0</is_reserved>
      <is_active>1</is_active>
    </OptionGroup>
</OptionGroups>
<OptionValues>
   <OptionValue>
      <label>Direct Action</label>
      <name>Direct Action</name>
      <filter>0</filter>
      <is_default>0</is_default>
      <weight>29</weight>
      <is_optgroup>0</is_optgroup>
      <is_reserved>0</is_reserved>
      <is_active>1</is_active>
      <option_group_name>activity_type</option_group_name>
    </OptionValue>
    <OptionValue>
      <label>Door Knock</label>
      <name>Door Knock</name>
      <filter>0</filter>
      <is_default>0</is_default>
      <weight>28</weight>
      <description>Door Knock</description>
      <is_optgroup>0</is_optgroup>
      <is_reserved>0</is_reserved>
      <is_active>1</is_active>
      <option_group_name>activity_type</option_group_name>
    </OptionValue>
    <OptionValue>
      <label>Letter of Inquiry</label>
      <name>Letter of Inquiry</name>
      <filter>0</filter>
      <is_default>0</is_default>
      <weight>23</weight>
      <is_optgroup>0</is_optgroup>
      <is_reserved>0</is_reserved>
      <is_active>1</is_active>
      <option_group_name>activity_type</option_group_name>
    </OptionValue>
</OptionValues>
</CustomData>

So you can specify multiple option groups and option values as well as custom data group and custom data fields and load them in from

(save the xml file as custom_data.xml)

then go to this url

http://<drupal_url>/sites/all/modules/civicrm/bin/migrate/import.php?name=loginName&pass=yourPW&key=yourSiteKey&file=<your_site_path_to>/custom_data.xml


You can see the relationship, you have to create custom group then custom field then option group -> option values.


Hope this helps!

Cheers!

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Defining multiple options with PHP
July 11, 2010, 07:00:55 pm
What about if you want to use PHP to define your option list?  This is easily done in Drupal CCK: just paste in any code that returns an associative array, and you're done. Is anything like that possible in Civi?

My scenario is that I want a drop-down list when choosing what semester someone attended at our semester program.  The options would be something like: Fall 2009 | Spring 2010 | Fall 2010 etc.  Entering the options manually wouldn't be a big deal, but of course the list needs to keep growing - every 6 months we would have to add a new option (and whoever's doing it would need to be consistent - that's my worry about manually entered data).

Again, this was super easy in Drupal. Here's the code from the CCK field "Semester":
Code: [Select]
/**
* Generate list of semesters from 2005 to *now*
* Please resist the temptation to limit this list to more recent semesters here,
* as it will break the display of older entries. Use hook_form_alter on the node form instead.
*/

$month = date('m');
$semester = ($month < 9 ? 1 : 2);
$year = date('Y');

$y = 2005;
$s = 1;
$value = array();

while ($y <= $year){

$sem = ($s == 1 ? 'Spring ' : 'Fall ');

$value[10*$y+$s] = $sem.$y;

$s++;
if ($s >= 3){
$s = 1;
$y++;
}
if ($y >= $year && $s > $semester) $y++;
}

return $value ;
This returns an associative array like:
20051 | Spring 2005
20052 | Fall 2005
20061 | Spring 2006... all the way up to the present.

So I'm looking for a similar idiot-proof way to do it in Civi. Relying on humans to manually add new options to the list on a regular basis just sounds like it's asking for trouble. Any ideas?

Thanks everyone for your help, and I don't mean to hijack this thread, my question seemed relevant (and maybe even helpful) to the above questions.
Try asking your question on the new CiviCRM help site.

xcf33

  • I post frequently
  • ***
  • Posts: 181
  • Karma: 7
  • CiviCRM version: 3.3.2
  • CMS version: Drupal 6.19/6.20
  • MySQL version: 5.x
  • PHP version: 5.2.6
Re: Importing to multiple choice fields
July 12, 2010, 01:38:30 pm
You will need a script similar to what you have written and have it update either the option_list table directly (I would do it but probably not recommend it) on a regular basis (via drupal cron or other cron jobs) or have your script write a XML file somewhere with the format I have posted above and do a cron of importing the XML into CiviCRM.

What you are talking about is some sort of dynamic list which I don't know how you can create other than what I stated above or use your logic to generate quick form element directly. (That would have nothing to do with option list, which are static)


Hope it helps

petednz

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4899
  • Karma: 193
    • Fuzion
  • CiviCRM version: 3.x - 4.x
  • CMS version: Drupal 6 and 7
Re: Importing to multiple choice fields
July 12, 2010, 02:11:15 pm
The more ho-hum approach might be
- create the Custom Group via the UI
- add one of the Custom Field options via the UI
- via Phpmyadmin, export that row
- replicate the rows with all your options in a spreadsheet
- convert to a csv and import back in to table.
Sign up to StackExchange and get free expert advice: https://civicrm.org/blogs/colemanw/get-exclusive-access-free-expert-help

pete davis : www.fuzion.co.nz : connect + campaign + communicate

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Importing to multiple choice fields
July 12, 2010, 02:22:56 pm
@xcf33
I had been thinking about the cron option... is it possible to add multiselect options via api?  That would be super easy to code.
Try asking your question on the new CiviCRM help site.

xcf33

  • I post frequently
  • ***
  • Posts: 181
  • Karma: 7
  • CiviCRM version: 3.3.2
  • CMS version: Drupal 6.19/6.20
  • MySQL version: 5.x
  • PHP version: 5.2.6
Re: Importing to multiple choice fields
July 12, 2010, 03:04:20 pm
No, there's no API for it, You will have to use the DAO for the database operations for option list and option value.

I personally think what you are trying to do is probably better suited using quick form to generate the form elements since its nature is dynamic.

Unfortunately I don't have a lot of quick form knowledge :(

ericaordinary

  • Guest
Re: Importing to multiple choice fields
July 19, 2010, 10:39:01 pm
Hi everyone,

I've just managed to import a sample set of options using xml (as suggested by xcf33, thanks!).  I thought I'd post my xml code to help out others, as there were a few things missing from the snippet I was working from:

Code: [Select]
<?xml version="1.0" encoding="iso-8859-1" ?>
<CustomData>

<CustomGroups>
    <CustomGroup>
      <name>test_network_affiliations</name>
      <title>Test Network Affiliations</title>
      <extends>Organization</extends>
      <extends_entity_column_value>Network</extends_entity_column_value>
      <style>Tab</style>
      <collapse_display>0</collapse_display>
      <help_pre></help_pre>
      <help_post></help_post>
      <weight>9</weight>
      <is_active>1</is_active>
      <table_name>civicrm_value_test_network_affiliations</table_name>
      <is_multiple>0</is_multiple>
      <collapse_adv_display>0</collapse_adv_display>
    </CustomGroup>
</CustomGroups>

<CustomFields>
    <CustomField>
      <label>Test LGA</label>
      <data_type>String</data_type>
      <html_type>AdvMulti-Select</html_type>
      <is_required>0</is_required>
      <is_searchable>1</is_searchable>
      <is_search_range>0</is_search_range>
      <weight>50</weight>
      <is_active>1</is_active>
      <is_view>0</is_view>
      <text_length>255</text_length>
      <note_columns>60</note_columns>
      <note_rows>4</note_rows>
      <column_name>test_lga</column_name>
      <option_group_name>test_lga_import_options</option_group_name>
      <custom_group_name>test_network_affiliations</custom_group_name>
    </CustomField>
</CustomFields>

<OptionGroups>
<OptionGroup>
      <name>test_lga_import_options</name>
      <label>Test LGAs</label>
      <is_reserved>0</is_reserved>
      <is_active>1</is_active>
    </OptionGroup>
</OptionGroups>

<OptionValues>
   <OptionValue>
      <label>Test LGA 1</label>
      <value>test_lga_1</value>
      <name>test_lga_1</name>
      <filter>0</filter>
      <is_default>0</is_default>
      <weight>1</weight>
      <is_optgroup>0</is_optgroup>
      <is_reserved>0</is_reserved>
      <is_active>1</is_active>
      <option_group_name>test_lga_import_options</option_group_name>
    </OptionValue>
    <OptionValue>
      <label>Test LGA 2</label>
      <value>test_lga_2</value>
      <name>test_lga_2</name>
      <filter>0</filter>
      <is_default>0</is_default>
      <weight>2</weight>
      <is_optgroup>0</is_optgroup>
      <is_reserved>0</is_reserved>
      <is_active>1</is_active>
      <option_group_name>test_lga_import_options</option_group_name>
    </OptionValue>
    <OptionValue>
      <label>Test LGA 3</label>
      <value>test_lga_3</value>
      <name>test_lga_3</name>
      <filter>0</filter>
      <is_default>0</is_default>
      <weight>3</weight>
      <is_optgroup>0</is_optgroup>
      <is_reserved>0</is_reserved>
      <is_active>1</is_active>
      <option_group_name>test_lga_import_options</option_group_name>
    </OptionValue>
</OptionValues>

</CustomData>

It ended up being pretty easy to go through the relevant tables in phpmyadmin for examples and copy the values I needed into the xml file.

I originally had a go at just importing just the second half (option group and option values), and while it imported the group of values to the DB I couldn't select this group for use in a custom data field within the CiviCRM UI.

My next step will be getting some help writing a small program to generate the option values within the xml file.

Peter, I originally tried out your method but exporting the row to a CSV ended up being more confusing than the xml route.

Thanks everyone for your help!

ericaordinary

  • Guest
Re: Importing to multiple choice fields
July 22, 2010, 11:21:15 pm
In the interests of further sharing, here's the php to generate the .xml file from a 1 column CSV:

Code: [Select]
<CustomData>

<CustomGroups>
    <CustomGroup>
      <name>test_network_affiliations</name>
      <title>Test Network Affiliations</title>
      <extends>Organization</extends>
      <extends_entity_column_value>Network</extends_entity_column_value>
      <style>Tab</style>
      <collapse_display>0</collapse_display>
      <help_pre></help_pre>
      <help_post></help_post>
      <weight>9</weight>
      <is_active>1</is_active>
      <table_name>civicrm_value_test_network_affiliations</table_name>
      <is_multiple>0</is_multiple>
      <collapse_adv_display>0</collapse_adv_display>
    </CustomGroup>
</CustomGroups>

<CustomFields>
    <CustomField>
      <label>Test LGA</label>
      <data_type>String</data_type>
      <html_type>AdvMulti-Select</html_type>
      <is_required>0</is_required>
      <is_searchable>1</is_searchable>
      <is_search_range>0</is_search_range>
      <weight>50</weight>
      <is_active>1</is_active>
      <is_view>0</is_view>
      <text_length>255</text_length>
      <note_columns>60</note_columns>
      <note_rows>4</note_rows>
      <column_name>test_lga</column_name>
      <option_group_name>test_lga_import_options</option_group_name>
      <custom_group_name>test_network_affiliations</custom_group_name>
    </CustomField>
</CustomFields>

<OptionGroups>
<OptionGroup>
      <name>test_lga_import_options</name>
      <label>Test LGAs</label>
      <is_reserved>0</is_reserved>
      <is_active>1</is_active>
    </OptionGroup>
</OptionGroups>

<OptionValues>
<?php
$row 
= 1;
if ((
$handle = fopen("testdata.csv", "r")) !== FALSE) {
    while ((
$data = fgetcsv($handle)) !== FALSE) {
        
$num = count($data);
$row++
?>

   <OptionValue>
<?php
        
for ($c=0; $c < $num; $c++) {
            echo 
"      <label>" . $data[$c] . "</label>\n";
            echo 
"      <value>" . strtolower(str_replace(' ', '_', $data[$c])) . "</value>\n";
            echo 
"      <name>" . strtolower(str_replace(' ', '_', $data[$c])) . "</name>\n";
        }
?>

      <filter>0</filter>
      <is_default>0</is_default>
      <weight><?php echo $row; ?></weight>
      <is_optgroup>0</is_optgroup>
      <is_reserved>0</is_reserved>
      <is_active>1</is_active>
      <option_group_name>test_lga_import_options</option_group_name>
    </OptionValue>

<?php
    
}
    
fclose($handle);
}

?>

</OptionValues>

</CustomData>


I got help from my (awesome and generous!) significant other with writing the program so can't take all the credit.   It should help out anyone else who wants to do something similar.

xcf33

  • I post frequently
  • ***
  • Posts: 181
  • Karma: 7
  • CiviCRM version: 3.3.2
  • CMS version: Drupal 6.19/6.20
  • MySQL version: 5.x
  • PHP version: 5.2.6
Re: Importing to multiple choice fields
July 23, 2010, 12:00:29 pm
Interesting idea,

using CSV to import fields, profiles and other things might actual be a pretty good idea. It makes it easier than XML which can look overwhelming

Pages: [1] 2
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Import (Moderator: Yashodha Chaku) »
  • Importing to multiple choice fields

This forum was archived on 2017-11-26.