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) »
  • Possible issue when trying to set the creator of an activity trough the API
Pages: [1]

Author Topic: Possible issue when trying to set the creator of an activity trough the API  (Read 404 times)

capo

  • I post occasionally
  • **
  • Posts: 108
  • Karma: 5
Possible issue when trying to set the creator of an activity trough the API
February 29, 2016, 05:21:55 am
First of all, hi people! Long time no see! I've been out for a while but I'm back, working with Ixiam, so happy to be back working with the CiviCRM community.  :)

There is something I need some advice with. I'm trying to create an activity through an API call and set the id of the user who created it. Something like:

Code: [Select]
$result = civicrm_api3('Activity', 'create', array(
  'sequential' => 1,
  'activity_type_id' => "Reminder Conversion",
  'api.ActivityContact.create' => array('contact_id' => 203, 'record_type_id' => 2),
));

That creates the activity properly, but it creates 2 records in civicrm_activity_contact with record_type_id = 2:
  • One of them with 203 as the contact_id
  • The other one with the id of the user I was logged with as its contact_id
The question is that it looks to me like there are two cases:
  • We are allowed to set the creator of an activity. In witch case, it shouldn't have created the second record with the id of the user I was logged with.
  • We are not supposed to be allowed to set the creator of an activity. In witch case, I guess the API call I wrote should raise an error.
What do you think about it? Is there another way to do what I'm trying?

Note: Because of the context, I need to create the activity contact while I create an activity, as a chained call. The context is similar to this alterApiParameters in CiviRules.

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: Possible issue when trying to set the creator of an activity trough the API
February 29, 2016, 12:00:14 pm
The Activity API also accepts some "magic" fields: "source_contact_id", "target_contact_id", and "assignee_contact_id". For this particular use-case, I've always used those fields (and haven't really tried chaining).

For example: If you omit "source_contact_id", then it defaults to the currently-logged in user. If you specify "source_contact_id", then it's set to the chosen contact.

capo

  • I post occasionally
  • **
  • Posts: 108
  • Karma: 5
Re: Possible issue when trying to set the creator of an activity trough the API
March 01, 2016, 02:52:56 am
Oooook! Actually way cleaner! I knew about target and assignee but didn't know about the source magic field. Works perfectly!

Thanks!

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Possible issue when trying to set the creator of an activity trough the API

This forum was archived on 2017-11-26.