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

Author Topic: Activity API: custom data?  (Read 1340 times)

TwoMice

  • I post frequently
  • ***
  • Posts: 214
  • Karma: 16
    • Emphanos
  • CiviCRM version: Always current stable version
  • CMS version: Drupal 7
Activity API: custom data?
February 20, 2014, 08:37:55 am
Hi All,

I have good results using custom data fields (as returns, and as parameters) for Contact 'get' operations. However, no such luck with Activity 'get' operations.  I've been reading over this wiki page (https://wiki.civicrm.org/confluence/display/CRMDOC/Using+Custom+Data+with+the+API), and it seems like this should work, but no luck.

In the API Explorer, I tried this URL: /civicrm/ajax/rest?entity=Activity&action=get&debug=1&sequential=1&json=1&custom_2=50058

What I get is a return of the first 25 activities, with no filtering on custom_2.
(BTW, custom_2 is a contact reference field; 50058 is the contact_id of a contact referenced in that field in exactly 1 activity.)

Also, I've tried getting the value of custom_2 when getting a specfic activty by activity_id, but the return value does not include the custom field. 
In the API Explorer: /civicrm/ajax/rest?entity=Activity&action=get&debug=1&sequential=1&json=1&activity_id=1296591&return[custom_2]=1
(Activity ID 1296591 is an existing activity with contact 50058 referenced in the custom_2 field.)

So, should I be going about this another way?

Thanks!
Allen
« Last Edit: February 20, 2014, 08:43:24 am by TwoMice »
Please consider contributing to help improve CiviCRM with the Make it Happen! initiative.

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: Activity API: custom data?
February 20, 2014, 11:08:17 am
1) I think the general recommendation has been to use the CustomValue API when filtering on custom-fields for any entity besides "Contact". I think that would work for your example, but this has a limitation -- it doesn't allow complex queries that filter on both core fields and custom fields.

2) There's some code here which purports to return custom-data for activities (~ L283 - L292):

https://github.com/civicrm/civicrm-core/blob/4.4/api/v3/Activity.php#L283

3) I don't know if anyone's tried, but based on my finite understanding, there's an interesting place where one might introduce generic support for filtering on single-row custom fields -- _civicrm_api3_dao_set_filter():

https://github.com/civicrm/civicrm-core/blob/master/api/v3/utils.php#L505

If I understand correctly, this is used by most of the "get" APIs (except Contact.get). However, it might not work because it represents the query with $dao and you need to add a dynamic JOIN for the custom-data table... and $dao sucks for adding dynamic JOINs.

4) If you really need complex queries, it might be easier to write your own API (like I mentioned in the other thread).

TwoMice

  • I post frequently
  • ***
  • Posts: 214
  • Karma: 16
    • Emphanos
  • CiviCRM version: Always current stable version
  • CMS version: Drupal 7
Re: Activity API: custom data?
February 20, 2014, 01:48:49 pm
Quote
write your own API (like I mentioned in the other thread)
Reference: http://forum.civicrm.org/index.php/topic,31718.msg135922.html#msg135922
Please consider contributing to help improve CiviCRM with the Make it Happen! initiative.

TwoMice

  • I post frequently
  • ***
  • Posts: 214
  • Karma: 16
    • Emphanos
  • CiviCRM version: Always current stable version
  • CMS version: Drupal 7
Re: Activity API: custom data?
February 20, 2014, 02:30:22 pm
totten:
Quote
I think the general recommendation has been to use the CustomValue API when filtering on custom-fields for any entity besides "Contact"

Yes, I think that would work for our use case. Am I right to think that this would require chaining? All the chaining-related API documentation I find seems to be aimed at 'create' functions; any reference to docs on how to do what you're describing?

Thanks,
A.
Please consider contributing to help improve CiviCRM with the Make it Happen! initiative.

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: Activity API: custom data?
February 20, 2014, 02:51:53 pm
api/v3/examples comes to mind:

https://github.com/civicrm/civicrm-core/tree/4.4/api/v3/examples/CustomValue
https://github.com/civicrm/civicrm-core/blob/4.4/api/v3/examples/Contact/APIChainedArray.php
https://github.com/civicrm/civicrm-core/blob/4.4/api/v3/examples/Contact/APIChainedArrayFormats.php
https://github.com/civicrm/civicrm-core/blob/4.4/api/v3/examples/Contact/APIChainedArrayMultipleCustom.php

TwoMice

  • I post frequently
  • ***
  • Posts: 214
  • Karma: 16
    • Emphanos
  • CiviCRM version: Always current stable version
  • CMS version: Drupal 7
Re: Activity API: custom data?
February 21, 2014, 12:26:44 pm
Thanks for the links, totten.

Based on what I'm reading those examples, I thought something like this might work.

Intent: find activities where contact 50058 is referenced in the custom_2 custom data field.
Rest URL: /civicrm/ajax/rest?entity=Activity&action=get&api.customvalue.get[id]=2&api.customvalue.get[entity_id]=$value.id&api.customvalue.get[value]=50058

But that just returns the first 25 activities, each one with this appended:
Code: [Select]
    <api_customvalue_get>
        <is_error>1</is_error>
        <error_message>API (customvalue, get) does not exist (join the API team and implement it!)</error_message>
    </api_customvalue_get>

Note the comma in "API (customvalue, get) does not exist". That looks suspicious, but I'm not sure what to make of it.

I'm open to any further advice on making this work.

Thanks!

- Allen
Please consider contributing to help improve CiviCRM with the Make it Happen! initiative.

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: Activity API: custom data?
February 21, 2014, 12:44:58 pm
It would be worth trying with "custom_value" or "CustomValue" rather than "customvalue". APIv3 tries to be clever when the entity/action includes multiple words, and the resulting behavior is something... which I can never remember. So whenever I get a message like that, I start playing around with the entity/action name.

FWIW, when submitting complex data structures, it usually clearer to me to submit the params as JSON ("&json={'foo':{'bar':['whiz','bang']}}") rather than figuring how to format GET parameters ("&foo[bar][0]=whiz&foo[bar][1]=bang" or somesuch).

TwoMice

  • I post frequently
  • ***
  • Posts: 214
  • Karma: 16
    • Emphanos
  • CiviCRM version: Always current stable version
  • CMS version: Drupal 7
Re: Activity API: custom data?
February 24, 2014, 02:35:19 pm
Good tip on the JSON parameters. Thanks, Tim.

I was finally able to get the custom field value, but not by any means relying on the "return" parameter. Instead, something like this does work for a given activity:

/civicrm/ajax/rest?entity=Activity&action=get&json={"id":1296591,"api.CustomValue.get":{"id":2}}

But filtering activities on custom values seems to be a non-starter at present. I'm giving up for now and may come back to it later.

Thanks again for your help.

- Allen
Please consider contributing to help improve CiviCRM with the Make it Happen! initiative.

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Activity API: custom data?
February 24, 2014, 11:30:47 pm
@allen,

if you are using class.api.php on your client side (ie. if you are using php), it should hide all the joy of figuring out what is the right param to put where. Mostly.

indeed, filtering on custom values would likely mean extending the generic basic get (that does what DAO offers, ie not so much join) or rewrite the activity.get).

Keep in mind that chaining api isn't a proper join, ie for each activity, you get an extra query to fetch the custom value. It's fine enough if you have a handful, but deadly if you aim at higher quantities.

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

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

This forum was archived on 2017-11-26.