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) »
  • Correct API call to update existing contact? (contact_create)
Pages: [1]

Author Topic: Correct API call to update existing contact? (contact_create)  (Read 1376 times)

greenmachine

  • I post occasionally
  • **
  • Posts: 58
  • Karma: 6
Correct API call to update existing contact? (contact_create)
May 01, 2011, 06:14:57 pm
This is a stupid question, but I cannot figure this out from v3 documentation or looking at the API code. What is the correct combination of $params attributes and API v3 function arguments to update an existing contact?

I have used civicrm_api('contact','create', $params) to successfully create new contacts. However i cannot get "update" behavior that I want. If $params['dupe_check'] = FALSE, duplicate contacts are created. If $params['dupe_check'] = TRUE, then no contact is created or updated and instead I am returned an error (Found matching contacts).

Can someone provide a code snippet? This seems like it should be easy but I'm missing something.  Here is an example of my $params variable passed to civicrm_api('contact', 'create', $params):

Array
(
    [custom_11] => 4908294
    [first_name] => Firstname
    [middle_name] => L
    [last_name] => Lastname
    [custom_142] => 09/09/1980
    [birth_date] => 01/01/1926
    [version] => 3
    [contact_type] => Individual
    [contact_id] => 2997
)

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Correct API call to update existing contact? (contact_create)
May 01, 2011, 06:15:43 pm
Try with 'id' rather than contact id.
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

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Correct API call to update existing contact? (contact_create)
May 01, 2011, 06:18:51 pm
NB - you are using 'version' => 3 aren't you?
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: Correct API call to update existing contact? (contact_create)
May 01, 2011, 11:27:19 pm
Quote from: greenmachine on May 01, 2011, 06:14:57 pm
This is a stupid question, but I cannot figure this out from v3 documentation or looking at the API code. What is the correct combination of $params attributes and API v3 function arguments to update an existing contact?

To help answering that question we have installed an API explorer, you visit /civicrm/ajax/doc on your site, choose Contact + get, to have some examples of what is returned. You can use api.Contact.getfields to see what fields exist, and then try contact+create, click on the various fields, start filling see if you can find the right combination. (after a few trial, tend to modify directly the url as generated and press enter, faster).

More explanations: http://civicrm.org/API_version_3

According to the error message, seems that the issue is the contact_type is missing. That's something we have to decide how to fix and we need more test, but probably that if there is a params[id] we should bypass all the other tests for mandatory.

You can also use

/civicrm/ajax/rest?json=1&debug=1&version=3&entity=Contact&action=update&id=1&first_name=new

$results=civicrm_api("Contact","update", array ('version' =>'3', 'id' =>'1', 'first_name' =>'new'));
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

greenmachine

  • I post occasionally
  • **
  • Posts: 58
  • Karma: 6
Re: Correct API call to update existing contact? (contact_create)
May 03, 2011, 10:12:17 pm
Thanks. Wow, I don't know how I managed not to find either the AJAX explorer or that info page (http://civicrm.org/API_version_3).

One problem: I am not seeing the input fields promised by the video. I was going to file a bug report but then it seems like both 3.4 and 4.0 demo sites display the same behavior. Is it intentional? Here's what I'm seeing:

On the 3.4 demo's AJAX explorer (http://drupal.demo.civicrm.org/civicrm/ajax/doc#explorer), input fields are not provided for API actions where input is possible. For example:

-- http://drupal.demo.civicrm.org/civicrm/ajax/doc#explorer
-- Choose "Contact" for entity
-- Choose "Create" for action
-- Wait for possible fields/input options to show up
-- Click "Internal Contact ID" (or any other field)
-- Internal Contact ID shows up in a list under the options, but no INPUT element is provided alongside it (based on video here: http://civicrm.org/API_version_3)

So there is no obvious way to provide input to test the create/update actions via the explorer.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Correct API call to update existing contact? (contact_create)

This forum was archived on 2017-11-26.