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) »
  • civicrm_api('contact','create') - how to create only if not duplicate contact
Pages: [1]

Author Topic: civicrm_api('contact','create') - how to create only if not duplicate contact  (Read 984 times)

tog22

  • I’m new here
  • *
  • Posts: 20
  • Karma: 0
  • CiviCRM version: CiviCRM 4
  • CMS version: Drupal 7
  • MySQL version: Varies
  • PHP version: Varies
civicrm_api('contact','create') - how to create only if not duplicate contact
March 05, 2012, 04:35:27 am
I'm creating a contact through the API with the following code:

Code: [Select]
$params = array(
  'first_name' => 'abc3',
  'last_name' => 'xyz3',
  'contact_type' => 'Individual',
  'email' => 'man3@yahoo.com',
  'version' => 3,
);
  $result = civicrm_api( 'contact','create',$params );

Each time I run this code a new contact is created with the same details. However, http://book.civicrm.org/user/working-with-your-data/deduping-and-merging says "Default strict rules are also automatically checked when new contacts are created through online registrations including events, membership, contributions, and profile pages, and when you create a contact through CiviCRM's programming API." And my CiviCRM install still has the default Individual-Strict dedupe rule which looks for matching email addresses. So this suggests that a new contact with the same email address should not be created the 2nd time I run the above code.

How can I create a contact through the API only if it doesn't match the email address of an existing contact?

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: civicrm_api('contact','create') - how to create only if not duplicate contact
March 07, 2012, 12:51:12 am
I believe the general principle of the API is: the API assumes you have checked your data. So there is not a lot of validation in the API. The aim is to keep the API flexible and robust. In my opinion this would mean that you have to develop a little wrapper function yourself that:
  • uses the API get to check if the contact already exists
  • only create the contact with the API if it does not

I do agree that if I am not corrected by some of my more knowledgeable community colleagues the text in the book needs to be updated!
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: civicrm_api('contact','create') - how to create only if not duplicate contact
March 07, 2012, 02:29:28 am
Try setting

  $params['dupe_check'] = TRUE ;

It's not tested / documented but I think it might work (it did once in v2 & I think it's from the BAO level not the v2 API)
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

tog22

  • I’m new here
  • *
  • Posts: 20
  • Karma: 0
  • CiviCRM version: CiviCRM 4
  • CMS version: Drupal 7
  • MySQL version: Varies
  • PHP version: Varies
Re: civicrm_api('contact','create') - how to create only if not duplicate contact
March 07, 2012, 03:11:56 am
Thanks,but $params['dupe_check'] = TRUE doesn't work in v3. I ended up checking manually though, as Erik suggested, since if the contact did exist I needed to add a contribution to it:

Code: [Select]
$cp = array('email' => $email, 'version' =>3);
$existing_contact = civicrm_api('Contact','Get',$cp);
if(empty($existing_contact['values'])){
// add a contribution to this contact
}

« Last Edit: March 07, 2012, 03:28:20 am by tog22 »

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • civicrm_api('contact','create') - how to create only if not duplicate contact

This forum was archived on 2017-11-26.