3

I have defined a custom field of type Integer Select. How can I import a list of integers into the dropdown options list (rather than enter them manually, one at a time)?

3 Answers 3

2

There are several extensions designed to do exactly this.

First is the aptly-named Option Values Importer. It hasn't been updated recently, and it only does the one thing - but it may be simpler to use as a consequence.

There's the configitems extension. This one has a bit more setup, but is great if you have to add/remove values on a regular basis.

Finally, there's the CSV API GUI extension. This is an alternative to the "API Explorer" approach Pete suggests, that lets you import in bulk from a CSV file. This works with all modern versions of CiviCRM and is probably the best bet if you don't want to mess with putting files into your web server's file system.

3
  • Many thanks - a few promising options, particularly the last. Jan 21, 2017 at 11:02
  • 1
    I just tried the Options Values importer on CiviCRM 4.7 and it doesn't seem to work. It installs, but I get an error "Cannot read unrecognized property CRM_Core_Config::$maxImportFileSize." Jan 25, 2017 at 11:41
  • 1
    I have used the CSV API GUI extension to upload option lists, though, and it works like a charm. Jan 25, 2017 at 11:42
0

Is your motivation to transfer the configuration from test to production in a controlled way? A way to do this is to use the API that is part of CiviCRM. It contains a function Optionvalue for this purpose. The API can be called from PHP, drush or as a REST call. The best way to learn more is to use the API Explorer. You can find it via the following menu: Support -> Developer -> API explorer menu.

An example to add a soup to the Food preferences on the standard dataset is in PHP:

$result = civicrm_api3('OptionValue', 'create', array(
  'sequential' => 1,
  'option_group_id' => "soup_selection",
  'label' => "Onion Soup",
  'name' => "Onion_Soup",
  'value' => "onion",
  'weight' => 45,
 ));

Or directly with Drush:

drush cvapi OptionValue.create sequential=1 option_group_id="soup_selection" label="Onion Soup" name="Onion_Soup" value="onion" weight=45

1
  • Nice write up. Weight is usually not spelt "weigth" just in case that trips any one up
    – petednz - fuzion
    Jan 15, 2017 at 22:03
0

Just to add to the options above. for brave (and knowledgable) souls who always take backups before operating on production (to paraphrase Dan's comment below) you can also do as an sql insert. For a few you can just 'copy as sql' an existing row then modify and run as a query eg

INSERT INTOcivicrm_option_value(option_group_id,label,value,name,grouping,filter,is_default,weight,description,is_optgroup,is_reserved,is_active) VALUES (93, 'Broth', 'broth', 'Broth', NULL, NULL, 0, 1, NULL, 0, 0, 1);

And if you have a lot to do I tend to do in a spreadsheet and use Concatenate and then insert.

Also a screenshot for the API Explorer approach

enter image description here

1
  • 1
    I'd caution against operating on the SQL databases directly, except for brave souls and those who take backups before operating on production. ;-) Jan 25, 2017 at 11:44

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.