4

I hope this is a useful question + answer. I feel it's necessary to document some, um, quirks, of the API v3 around Activities and custom data, because as a developer this took me ages to figure out.

I want to fetch all the details of a single activity, including custom fields.

Currently (4.6.7):

  • The normal activity get methods omit the custom fields.
  • You can get the custom fields using some voodo, but then you don't get all the standard fields.
2

Let's say $params is enough to find the single activity, e.g. $params = ['activity_id' => 123] where 123 is the known Id. But it could be any other API acceptable params.

$result = civicrm_api3('activity', 'getsingle', $params)
        + civicrm_api3('activity', 'getsingle',
                       [return:'custom_%,activity_type_id'] + $params);

The custom_% results in all custom fields being added. But this won't work without activity_type_id also being requested.

(You probably know, but the $array1 + $array2 stuff in PHP means extend array 1 with key/value pairs from array 2 when the key from array 2 does not exist in array 1.)

Alternative method

You can, list every field in the activity table as well as the custom stuff and in the return parameter to one API call.

However, this assumes that you know all the fields and that they won't change over time/upgrades. So the 2-call method above ensures that you're getting all CiviCRM has to offer on your activities. (Although you have to do extra calls to get the related contacts like targets and assignees etc. of course). It could be argued that you should only request what you want anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.