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) »
  • API and custom data
Pages: [1]

Author Topic: API and custom data  (Read 1048 times)

artfulrobot.com

  • I post occasionally
  • **
  • Posts: 81
  • Karma: 4
API and custom data
May 18, 2012, 03:06:59 am
Hi

Trying to use the API, but having a few problems, reported here in case (a) it's useful to devs (b) it's useful to others in the community trying to use it like I am. This post refers to CiviCRM 4.1.2

1. Bugs in (otherwise brilliant tool) API explorer

It generates invalid Smarty. e.g. return.somegroup:some_field="1" will never work as a smarty parameter; you must go dig up the custom field id and submit return_custom_NN instead. (Also worth note that the groupname:fieldname format doesn't work for Contact get.)

It generates invalid PHP
$results=civicrm_api("Contact","get", array (version=3','sequential' =>'0','contact_id' =>'1'));


2. Requesting custom data via Contact get request only ever returns one result for the custom field

This might be designed behaviour for some reason, but it is not in the documentation http://wiki.civicrm.org/confluence/display/CRMDOC40/CiviCRM+Public+APIs and I'm not clear how it picks one.


3. Data from CustomValue get is difficult to work with - please confirm I'm doing the right thing

Requesting a particular custom field (which may exist multiple times) returns and object with a values key pointing to an array. With sequential set, you can then reference the first (only) element of this array at 0. But the object inside there looks like this:

{
      "entity_id":"1",
      "latest":"Bam Bam",
      "id":"2",
      "308":"Pebbles Flintstone",
      "309":"Bam Bam"
   }


Now assuming you wanted the values (Pebbles Flintstone and Bam Bam)  you have to somehow filter the keys/props of this array/object for numeric values (or more precisely, strings that only contain numbers). Or unset (PHP) / delete (javascript) the entity_id, latest and id fields, before enumeration. The latter is more efficient, but then if a later version of CiviCRM introduces other output it will start showing up to users as data. Also - there seems to be no way to either match on regex or delete (unassign) vars in Smarty(!).

This post: http://forum.civicrm.org/index.php/topic,21844.msg94159.html#msg94159 suggests the values are available in a different format (might be relevant for 1 or Zero case?).


« Last Edit: May 18, 2012, 03:36:17 am by artfulrobot.com »

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: API and custom data
May 18, 2012, 05:26:43 am
Hi,

thanks for the feedback (and compliments ;)

1. syntax is a bit different between smarty /php / ajax and the api explorer is ignoring some of these differences.

return=custom_a,first_name,last_name works for smarty & ajax (and that's the syntax I use)

The php syntax has been fixed for 4.2

2. You need to use the custom data api to fetch more than one. that's a bit like address, if you want the primary one, you can use api.contact.get, otherwise, you need to use address.

3. for custom fields format, we might need colemans joining the thread, I haven't used it enough to have a useful answer.

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

artfulrobot.com

  • I post occasionally
  • **
  • Posts: 81
  • Karma: 4
Re: API and custom data
May 18, 2012, 06:25:59 am
1. v. handy - could that be documented somewhere?

2. got it. Again, could it be documented?

3. yeah, it's taken me a long time to find a fix for this. Ended up writing a new Smarty plugin function - i.e. doing the job in PHP, wrapping it in Smarty. I'm much happier in PHP anyway. In terms of a suggestion for development, I would think that you either need a structure like:

{ entity_id: 123, latest: 'fred', values: { 321=>"fred", 987=>"barney" } }
i.e. put all the values in their own sub-object, so they can be easily enumerated.

OR (easier to bolt-on and backwards compatible, but inefficient)
{ entity_id: 123, latest: 'fred', keys: [321,987],  321=>"fred", 987=>"barney" }
i.e. list the keys so the values can be enumerated.



xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: API and custom data
May 18, 2012, 06:51:26 am
Quote from: artfulrobot.com on May 18, 2012, 06:25:59 am
1. v. handy - could that be documented somewhere?

2. got it. Again, could it be documented?


If you could update the wiki, would be greatly appreciated!
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

artfulrobot.com

  • I post occasionally
  • **
  • Posts: 81
  • Karma: 4
Re: API and custom data
May 18, 2012, 09:46:36 am
Here you go
http://wiki.civicrm.org/confluence/display/CRMDOC41/Using+Custom+Data+with+the+API

pls feel free to correct/add-to/fill out. Also I didn't want to remove it, but on that page there's a load of stuff from 2008 that isn't relevant to API v3. Someone with more authority should probably remove/clean that up.

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: API and custom data
May 18, 2012, 08:20:44 pm
Karma + for getting in there with the documentation

I did the most obvious edits I could see on the older notes (changing to civicrm_api()).

We did really struggle on how to format the custom value get stuff - the api syntax is that everything should be id-indexed but for custom values the id was only unique if you also included the group. You'll find long threads on this forum about this & obviously in the end we still didn't get it quite right - the problem is that all other API are based on a single table but not this 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

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

This forum was archived on 2017-11-26.