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 »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Creating Custom Values using API
Pages: [1]

Author Topic: Creating Custom Values using API  (Read 4865 times)

bpmccain

  • I post frequently
  • ***
  • Posts: 255
  • Karma: 5
  • CiviCRM version: 4.1
  • CMS version: Drupal 7.12
  • MySQL version: 5.2
  • PHP version: 5.2
Creating Custom Values using API
July 01, 2011, 12:52:38 pm
I'm trying to import a new Individual contact using the API. I can create the contact and the address no problem, but I am having trouble filling in the values for the custom fields that I have setup for Individual contacts.

I'm pretty sure my problem is around the keys. If I look in my database under civicrm_value_custom_group_name, I can see each row for custom values I have entered using the GUI. In the below code, the 'id' number I am using is the entity_id number from this table.

The other keys are the column headings from this table, but these are slightly different from the names in civicrm_custom_field table.

Any ideas? Thanks in advance

Code: [Select]
$additionalParams = array('version' => 3,
'id' => $contactID,
'official_provincial_riding_1' => $ridingNumber,
'official_provincial_riding_poll__6' => $pollNumber,
'official_provincial_riding_poll__10' => $pollSuffix,
'address_id_11' => $addressID);

$results=civicrm_api("CustomValue","create", $additionalParams);

Brian

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Creating Custom Values using API
July 01, 2011, 02:05:56 pm
Hi,

The customValue api is a new way we are looking at to do custom fields. Unfortunately a version made it into the release that I don't think is final (last seen I was hoping that Coleman and Xavier would agree the last few points).

However, for 90% of use cases the CustomValue api is not required as you can simply do this

$params = array('version' => 3,
                       'display_name' => 'Clarke Kent',
                      'custom_12' => 'red cape')
civicrm_api('contact','create',$params);

that will create a contact called 'Clarke Kent' with custom field # 12 set to red cape.

If you want to update an existing contact pass in the 'id' field

Eileen
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

bpmccain

  • I post frequently
  • ***
  • Posts: 255
  • Karma: 5
  • CiviCRM version: 4.1
  • CMS version: Drupal 7.12
  • MySQL version: 5.2
  • PHP version: 5.2
Re: Creating Custom Values using API
July 01, 2011, 02:44:46 pm
Thanks. I will give that a try.

bpmccain

  • I post frequently
  • ***
  • Posts: 255
  • Karma: 5
  • CiviCRM version: 4.1
  • CMS version: Drupal 7.12
  • MySQL version: 5.2
  • PHP version: 5.2
Re: Creating Custom Values using API
July 03, 2011, 09:11:13 am
Thanks Eileen, that worked great for me.

I also wasn't aware that you had to use custom_1 etc as the key instead of the actual name of the key (in my case official_provincial_riding_1).

Maybe this could be added to the documentation somewhere for the api. Happy to contribute to it, but not sure how.

Brian

JoeMurray

  • Administrator
  • Ask me questions
  • *****
  • Posts: 578
  • Karma: 24
    • JMA Consulting
  • CiviCRM version: 4.4 and 4.5 (as of Nov 2014)
  • CMS version: Drupal, WordPress, Joomla
  • MySQL version: MySQL 5.5, 5.6, MariaDB 10.0 (as of Nov 2014)
Re: Creating Custom Values using API
July 19, 2011, 10:21:39 am
Thanks for thinking of helping out with the documentation. It's always and important thing for open source projects like CiviCRM to constantly improve and polish.

There are various versions of the documentation. The inline documentation in the code can be editted then a patch file created capturing your changes with diff.

The best way is to set yourself up with subversion (http://wiki.civicrm.org/confluence/display/CRMDOC40/Installing+CiviCRM+from+subversion+%28SVN%29+repository) then after editting a code file, use a subversion tool or command to create the patch, eg:

$ cd [mydocroot]/sites/all/modules/civicrm
$ svn diff api/v3/Contact.php > mypatch.txt

To edit the wiki documentation, you need to register on the wiki, then login, then go to the page, eg http://wiki.civicrm.org/confluence/display/CRMDOC40/CiviCRM+Public+APIs and click the edit button.
Co-author of Using CiviCRM https://www.packtpub.com/using-civicrm/book

JoeMurray

  • Administrator
  • Ask me questions
  • *****
  • Posts: 578
  • Karma: 24
    • JMA Consulting
  • CiviCRM version: 4.4 and 4.5 (as of Nov 2014)
  • CMS version: Drupal, WordPress, Joomla
  • MySQL version: MySQL 5.5, 5.6, MariaDB 10.0 (as of Nov 2014)
Re: Creating Custom Values using API
July 19, 2011, 10:29:29 am
To edit the FLOSS Manual, go to http://booki.flossmanuals.net/, create an account, then sign in, click on the CiviCRM book's link, and edit the relevant section of the book. Not sure if there is an approval process on the privileges for editting the book. If so, try contact michaelmcandrew
Co-author of Using CiviCRM https://www.packtpub.com/using-civicrm/book

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Creating Custom Values using API
July 19, 2011, 03:45:39 pm
I think it would make sense to include a module like the once Brian has just written in Core called something like api_example_module which just registers a URL & does 1 api get call when you go to that URL if you turn it on.
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Kirk

  • I post occasionally
  • **
  • Posts: 46
  • Karma: 2
  • CiviCRM version: 4.5.8
  • CMS version: Drupal 7.39
  • MySQL version: 5.5.44
  • PHP version: 5.6.13
Re: Creating Custom Values using API
July 21, 2011, 08:40:54 am
Hi bpmccain,

Your attempt to use the CustomValue API failed because the parameters were not correct.

There are different ways you can specify custom fields - with IDs, names or labels. However, it looks like you tried using column names (civicrm_custom_field.column_name) which is not valid.

You could have used database IDs (civicrm_custom_field.id) thus:

    $additionalParams = array(
        'version' => 3,
        'id' => $contactID,
        'custom_1' => $ridingNumber,
        'custom_6' => $pollNumber,
        'custom_10' => $pollSuffix,
        'custom_11' => $addressID
    );

Alternatively you could have identified the field using its name (civicrm_custom_field.name) or label (civicrm_custom_field.label). With either of these you must also supply an argument to identify the group to which the field belongs using the group's name (civicrm_custom_group.name) or title (civicrm_custom_group.title). The arguments are separated by colons, so your code would look something like this:

    $additionalParams = array(
        'version' => 3,
        'id' => $contactID,
        'custom_riding_number:official_provincial_riding' => $ridingNumber,
        'custom_poll_number:official_provincial_riding_poll' => $pollNumber,
        'custom_poll_suffix:official_provincial_riding_poll' => $pollSuffix,
        'custom_address_id:address' => $addressID
    );

This assumes that the groups are not multi-valued. If they are then you need to add an extra numerical argument; the ID of the custom value record, or -1 for a new value, e.g.:

        'custom_poll_number:official_provincial_riding_poll:-1' => $pollNumber,

Group name/title can be left blank if the field name is globally unique, e.g.:

        'custom_poll_number::-1' => $pollNumber,

Some of this I got from the comments at the top of civicrm/api/v3/CustomValue.php. Unfortunately these comments are incomplete and misleading. They state that arguments should be supplied in the order of group_name:field_name but I had to look into the code of that file and of civicrm/CRM/Core/BAO/CustomField.php to determine that the expected order is actually field_name:group_name.

I must say I find it disappointing that CiviCRM 4 was released with the custom fields API so unfinished and undocumented. This problem has cost me a lot of time and I'm sure I'm not the only one who's been struggling with it.

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Creating Custom Values using API
July 21, 2011, 12:35:22 pm

Hey kirk:

parts of api v3 are still a work in progress, but it has significantly improved the state of the API and its future.

The custom value API is still being discussed and there is an open issue:

http://issues.civicrm.org/jira/browse/CRM-8131

Would be great if you can help with that issue so others can be saved hours. That will help the project and the community

thanx

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Creating Custom Values using API
July 21, 2011, 01:35:18 pm
It's unfortunate CustomValue was included in a release because the version that was included was not the final one
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

ymmatt

  • I’m new here
  • *
  • Posts: 29
  • Karma: 0
Re: Creating Custom Values using API
August 25, 2011, 12:07:18 pm
So the above solution works well for fields that don't allow multiple entries, but what if they allow multiple entries, how do you add a new set of entries using the api? can you?

Edit:
Decided to take my own advice and "try it and see" and lo and behold it does what I want and adds a new record, very cool
« Last Edit: August 25, 2011, 03:00:29 pm by ymmatt »

artfulrobot.com

  • I post occasionally
  • **
  • Posts: 81
  • Karma: 4
Re: Creating Custom Values using API
September 02, 2011, 01:26:21 am
I get an exception when trying to set a custom field's value on an existing contact that has not had the custom field value set. It is not a multiple value field.

e.g. this API call (derived from API explorer)
example.org/civicrm/ajax/rest?version=3&custom_7=yay&id=105&fnName=civicrm%2FContact%2Fcreate&json=1

will set (update) custom field 7 to "yay" if contact 105 has previously had that field set to something, but if not I get "One of parameters (value: ) is not of the type Integer". custom_7 is not an integer field type. Looks like the UPDATE works but not the CREATE?

I'm on CiviCRM 4.0.5.

Looks buggy to me, (and Kurund + Michal couldn't figure it either!), but perhaps there's some designed reason for this?

Thanks.
Rich

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Creating Custom Values using API
September 02, 2011, 04:54:56 am
Hi,

I know it has been changed/fixed recently. Could you check on demo? You don't seem to have the latest version of civi.
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Creating Custom Values using API
September 05, 2011, 11:47:25 am
Recent changes have only focussed on 'GET' , not 'create'
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Creating Custom Values using API

This forum was archived on 2017-11-26.