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) »
  • Rest api chaining problem during create contact/emails/phones
Pages: [1]

Author Topic: Rest api chaining problem during create contact/emails/phones  (Read 762 times)

dbl81

  • I’m new here
  • *
  • Posts: 7
  • Karma: 0
  • CiviCRM version: 4.4
  • CMS version: drupal 7
  • MySQL version: 5
  • PHP version: 5.2
Rest api chaining problem during create contact/emails/phones
March 09, 2014, 05:11:51 pm
I'm trying to create a contact with emails and phones with one rest call by api chaining. I'm using the following fields for the rest call post:

Code: [Select]
$fields = array(
  'api_key' => <<apikey>>,
  'key' => <<key>>,
'entity' => 'contact',
'action' => 'create',
'contact_type' => 'Individual',
'email' => 'test@test.it',
'display_name' => 'test go west',
'first_name' => 'test',
'middle_name' => 'go',
'last_name' => 'west',
'birth_date' => '1981-06-04',
'gender_id' => '2',
'api.email.create' => array(
'location_type_id' => '2',
'email' => 'test@test.it',
'is_primary' => 0
),
'api.phone.create' => array(
array(
'phone_type_id' => '1',
'location_type_id' => '1',
'phone' => '123-456',
'phone_numeric' => '123456',
'is_prymary' => '1'
),
array(
'phone_type_id' => '1',
'location_type_id' => '2',
'phone' => '789-123',
'phone_numeric' => '789123',
'is_prymary' => '0'
)
)

);

and it works well.

Then I tried to insert more than one email like in the following code:

Code: [Select]
$fields = array(
  'api_key' => <<apiKey>>,
  'key' => <<key>>,
'entity' => 'contact',
'action' => 'create',
'contact_type' => 'Individual',
'email' => 'test@test.it',
'display_name' => 'test go west',
'first_name' => 'test',
'middle_name' => 'go',
'last_name' => 'west',
'birth_date' => '1981-06-04',
'gender_id' => '2',
'api.email.create' => array(
array(
'location_type_id' => '1',
'email' => 'test@test.it',
'is_primary' => 0
),
array(
'location_type_id' => '2',
'email' => 'test2@test.it',
'is_primary' => 0
        )
),
'api.phone.create' => array(
array(
'phone_type_id' => '1',
'location_type_id' => '1',
'phone' => '123-456',
'phone_numeric' => '123456',
'is_prymary' => '1'
),
array(
'phone_type_id' => '1',
'location_type_id' => '2',
'phone' => '789-123',
'phone_numeric' => '789123',
'is_prymary' => '0'
)
)

);

and I got the following error:

Code: [Select]
{"fields":["contact_id"],"error_code":"mandatory_missing","entity":"contact","action":"create","is_error":1,"error_message":"Error in call to phone_create : Mandatory key(s) missing from params array: contact_id"}

Do you see anything wrong in my code?
« Last Edit: March 09, 2014, 05:18:13 pm by dbl81 »

dbl81

  • I’m new here
  • *
  • Posts: 7
  • Karma: 0
  • CiviCRM version: 4.4
  • CMS version: drupal 7
  • MySQL version: 5
  • PHP version: 5.2
Re: Rest api chaining problem during create contact/email/phones
March 09, 2014, 05:16:51 pm
I also tried the following code:

Code: [Select]
$fields = array(
  'api_key' => <<apiKey>>,
  'key' => <<key>>,
'entity' => 'contact',
'action' => 'create',
'contact_type' => 'Individual',
'email' => 'test@test.it',
'display_name' => 'test go west',
'first_name' => 'test',
'middle_name' => 'go',
'last_name' => 'west',
'birth_date' => '1981-06-04',
'gender_id' => '2',
'api.email.create' => array(
array(
'contact_id' => '$value.id',
'location_type_id' => '1',
'email' => 'test@test.it',
'is_primary' => 0
),
array(
'contact_id' => '$value.id',
'location_type_id' => '2',
'email' => 'test2@test.it',
'is_primary' => 0
)
),
'api.phone.create' => array(
array(
'contact_id' => '$value.id',
'phone_type_id' => '1',
'location_type_id' => '1',
'phone' => '123-456',
'phone_numeric' => '123456',
'is_prymary' => '1'
),
array(
'contact_id' => '$value.id',
'phone_type_id' => '1',
'location_type_id' => '2',
'phone' => '789-123',
'phone_numeric' => '789123',
'is_prymary' => '0'
)
)

);

and I got the following error:

Code: [Select]
{"error_field":"contact_id","type":"integer","error_code":2001,"entity":"contact","action":"create","is_error":1,"error_message":"Error in call to phone_create : contact_id is not a valid integer"}

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: Rest api chaining problem during create contact/emails/phones
March 09, 2014, 06:17:11 pm
I didn't know that syntax ever worked, but there are two other syntaxes for the same use-case. You might try one of them:

 * "api.Entity.create" ==> eg https://github.com/civicrm/civicrm-core/blob/4.4/api/v3/examples/Contact/ChainTwoWebsitesSyntax2.php
 * "api.Entity.create.#" ==> eg https://github.com/civicrm/civicrm-core/blob/4.4/api/v3/examples/Contact/ChainTwoWebsites.php
 * "api.Entity.replace" ==> eg https://github.com/civicrm/civicrm-core/blob/4.4/tests/phpunit/api/v3/EmailTest.php#L244

FWIW, there's a typo in "is_prymary" -- may or may not be significant.

dbl81

  • I’m new here
  • *
  • Posts: 7
  • Karma: 0
  • CiviCRM version: 4.4
  • CMS version: drupal 7
  • MySQL version: 5
  • PHP version: 5.2
Re: Rest api chaining problem during create contact/emails/phones
March 11, 2014, 06:49:42 am
Totten thank you for your answer.
I solved my problem using the syntax "api.Entity.create.#".
I also understood why it didnt work with the first one (api.Entity.create) and it seems to be a little bug in the api.php code (that calls the chained api): I will post my testing when I have time (I hope in the next days).

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Rest api chaining problem during create contact/emails/phones

This forum was archived on 2017-11-26.