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.activity.create incorrectly adds id parameter for second activity
Pages: [1]

Author Topic: api.activity.create incorrectly adds id parameter for second activity  (Read 2642 times)

davej

  • Ask me questions
  • ****
  • Posts: 404
  • Karma: 21
api.activity.create incorrectly adds id parameter for second activity
January 23, 2012, 05:35:08 am
Hi,

Found what looks like a bug in api.activity.create in Civi 4.0.8 . I'm passing an array of 2 activities. The first is an update so I'm passing an id parameter. The second is a new activity, so I'm not passing the id parameter. However api.activity.create carries the id parameter over to the 2nd activity, so this acts as an update too. The net result is that the 2nd activity overwrites the 1st activity instead of creating a new one.

Parameters passed:

Code: [Select]
(
    [version] => 3
    [debug] => 1
    [contact_id] => 110815
    [contact_type] => Individual
    [has_js] => 1
    [check_permissions] => 1
    [subject] => Blah
    [api.activity.create] => Array
        (
            [0] => Array
                (
                    [activity_date_time] => 20120123122225
                    [custom_25] => 20110601103000
                    [subject] => Test-payment-completed-4
                    [custom_27] => 105003
                    [custom_26] => 20110501103140
                    [custom_28] => 250
                    [custom_29] => GBP
                    [activity_type_id] => 39
                    [status_id] => 2
                    [custom_18] => Enrolled
                    [id] => 495
                    [source_contact_id] => $value.id
                    [target_contact_id] => $value.id
                )

            [1] => Array
                (
                    [activity_date_time] => 20120123123054
                    [custom_25] => 20110801103000
                    [subject] => Test-payment-incomplete-5
                    [custom_27] => 100104
                    [custom_26] => 20110701103000
                    [custom_28] => 300
                    [custom_29] => EUR
                    [activity_type_id] => 39
                    [status_id] => 7
                    [custom_18] => Hot
                    [source_contact_id] => $value.id
                    [target_contact_id] => $value.id
                )

        )

)
Result:

Code: [Select]
{
"is_error":0,
"version":3,
"count":1,
"id":110815,
"values":{
"110815":{
"id":"110815",
"contact_type":"Individual",
"contact_sub_type":null,
"do_not_email":null,
"do_not_phone":null,
"do_not_mail":null,
"do_not_sms":null,
"do_not_trade":null,
"is_opt_out":null,
"legal_identifier":null,
"external_identifier":null,
"sort_name":"France8, Test",
"display_name":"Test France8",
"nick_name":null,
"legal_name":null,
"image_URL":null,
"preferred_communication_method":null,
"preferred_language":"en_GB",
"preferred_mail_format":null,
"hash":null,
"api_key":null,
"first_name":null,
"middle_name":null,
"last_name":null,
"prefix_id":null,
"suffix_id":null,
"email_greeting_id":null,
"email_greeting_custom":null,
"email_greeting_display":null,
"postal_greeting_id":null,
"postal_greeting_custom":null,
"postal_greeting_display":null,
"addressee_id":null,
"addressee_custom":null,
"addressee_display":null,
"job_title":null,
"gender_id":null,
"birth_date":null,
"is_deceased":null,
"deceased_date":null,
"household_name":null,
"primary_contact_id":null,
"organization_name":null,
"sic_code":null,
"user_unique_id":null,
"api.activity.create":[{
"is_error":0,
"version":3,
"count":1,
"id":495,
"values":[{
"id":"495",
"source_contact_id":"110815",
"source_record_id":null,
"activity_type_id":39,
"subject":"Test-payment-completed-4",
"activity_date_time":"20120123122225",
"duration":null,
"location":null,
"phone_id":null,
"phone_number":null,
"details":null,
"status_id":2,
"priority_id":null,
"parent_id":null,
"is_test":null,
"medium_id":null,
"is_auto":null,
"relationship_id":null,
"is_current_revision":null,
"original_id":null,
"result":null,
"is_deleted":null,
"campaign_id":null,
"engagement_level":null
}]
},
{
"is_error":0,
"version":3,
"count":1,
"id":495,
"values":[{
"id":"495",
"source_contact_id":"110815",
"source_record_id":null,
"activity_type_id":39,
"subject":"Test-payment-incomplete-5",
"activity_date_time":"20120123123054",
"duration":null,
"location":null,
"phone_id":null,
"phone_number":null,
"details":null,
"status_id":7,
"priority_id":null,
"parent_id":null,
"is_test":null,
"medium_id":null,
"is_auto":null,
"relationship_id":null,
"is_current_revision":null,
"original_id":null,
"result":null,
"is_deleted":null,
"campaign_id":null,
"engagement_level":null
}]
}]
}
}
}
Note that in the input params, id 495 is specified only for the first activity but in the result, it is specified for both activities.

Dave J

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: api.activity.create incorrectly adds id parameter for second activity
January 23, 2012, 05:55:05 am
Hi Dave,
that looks indeed like unwanted behaviour, and something that has not been taken care of. Would you care to raise a bug, and obviously a patch would be greatly appreciated........
thanks for reporting!
Erik
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

davej

  • Ask me questions
  • ****
  • Posts: 404
  • Karma: 21
Re: api.activity.create incorrectly adds id parameter for second activity
January 23, 2012, 06:32:21 am
Hi Erik,

Thanks for confirming. I've filed http://issues.civicrm.org/jira/browse/CRM-9526 .

Where does the api.create.blah magic happen?

[Edit:] A workaround is to specify id = null for the 2nd activity.

Cheers,

Dave J
« Last Edit: January 23, 2012, 06:40:42 am by davej »

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: api.activity.create incorrectly adds id parameter for second activity
January 23, 2012, 07:44:31 am
The api.create magic actually happens in api.php in the /civicrm/api folder, which actually calls the civicrm/api/v3/Activity.php....I am not sure where the issue is, I would make sure that the call is properly processed by the api.php first. Good luck, efforts much appreciated :-)
Cheers
Erik
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

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.activity.create incorrectly adds id parameter for second activity
January 23, 2012, 07:45:12 am
Hi Dave,

So you are using the api without a "main" call but as a way to bundle to actions? Interesting, we didn't think about that one, I like it ;)

The api.call start into api/api.php (civicrm_api)

The one you want is probably into _civicrm_api_call_nested_api and the specific create function is civicrm_api3_activity_create  in api/v3/Activity.php

The code is quite old and complicated (feels the v2 past), would be create to migrate it to a more modern technique (compare with api/v3/Survey.php ;) and leveraging _civicrm_api3_survey_create_spec

Anyway, rhe DAO as an unfortunate tendency of keeping quite a few things into memory/static/globals. It might be the reason it thinks the id is set on the second call. Haven't checked the code, just past experiences ;)

X+
-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: api.activity.create incorrectly adds id parameter for second activity
January 23, 2012, 01:07:06 pm
From memory the correct behaviour (in that nested call is)

if id is passed in then the id should be passed to the next call as entity_id

e.g. if you do a contact action then the id from that action is passed to the next entity as 'contact_id'

I *think* the business of passing 'id' down when it's the same entity was on purpose because the most common usage of nesting two calls to the same entity was get+delete.
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

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.activity.create incorrectly adds id parameter for second activity
January 26, 2012, 12:38:38 am
Hi,

parent id -> nested contact_id is the right thing to do indeed,

for what I understand, here that's between sub apis, not sure it's "normal" that the id of the first is "leaked" to the second one.



As a side note, I prefer  var_export ($v) as var_dump/print_r, found it more readable/easier to re-use for testing ;)

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

davej

  • Ask me questions
  • ****
  • Posts: 404
  • Karma: 21
Re: api.activity.create incorrectly adds id parameter for second activity
January 26, 2012, 02:48:29 am
Quote from: xavier on January 23, 2012, 07:45:12 am
So you are using the api without a "main" call but as a way to bundle to actions? Interesting, we didn't think about that one, I like it ;)

In general we're passing contact, address, phone & email parameters too, depending on what's passed to the calling function.

It sounds as though there's some debate about what the expected behaviour is. Anyway specifying id = null for the second activity works around it.

Thanks,

Dave J

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • api.activity.create incorrectly adds id parameter for second activity

This forum was archived on 2017-11-26.