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) »
  • Check if contact exists
Pages: [1] 2

Author Topic: Check if contact exists  (Read 4073 times)

ilbassa

  • I’m new here
  • *
  • Posts: 11
  • Karma: 0
  • CiviCRM version: 3.4.7
  • CMS version: Joomla 1.5
  • MySQL version: unknown
  • PHP version: 5.2.13
Check if contact exists
November 07, 2011, 08:46:59 am
Hi everyone, I'm modifying a module that insert a new contact.

I want to check if the contact already exist, my code is:
[ CODE ]
   global $got_email, $got_firstName, $got_lastName, $alreadyRegistered, $thankYou, $myError, $contact_id;
   
   $params ["email"] = $got_email;
   // Initiate CiviCRM
   require_once JPATH_ROOT.'/'.'administrator/components/com_civicrm/civicrm.settings.php';
   require_once 'CRM/Core/Config.php';
   $civiConfig =& CRM_Core_Config::singleton( );
   
   require_once 'api/v2/Contact.php';
   
//MY CODE
   //Check if exist
//THE PROBLEM HERE!
   $contact=& civicrm_contact_get($params);
   //Here I suppose there is only one contact with that address and retrieve it
   $contact=array_shift($contact);
//END MY CODE
   
   if(empty($contact)){
      //The contact doesn't exists it will be created
      $params ["first_name"] = $got_firstName;
      $params ["last_name"] = $got_lastName;
      $params ["contact_type"] = "Individual";
      $params ["dupe_check"] = "1";
      
      $contact =&civicrm_contact_add( $params );

      $myError = $alreadyRegistered;
      $contact_id = 0;
      if($contact["is_error"] == 0){
         $myError = $thankYou;
         $got_email = "";
         $got_firstName ="";
         $got_lastName = "";
         $antiSpamAnswer ="";
         $contact_id = $contact["contact_id"];
      }
   }else{
//MY CODE
      //The contact already exists. Retrieve the id
      $myError =$thankYou;
      $got_email = "";
      $got_firstName ="";
      $got_lastName = "";
      $antiSpamAnswer ="";
      $contact_id = $contact["contact_id"];
//END MY CODE
   }
   [/ CODE]

The module works well in a Joomla 1.5 local version, but when i upload it in my production site the module produce a blank page (probably with error 500).

If I  put a comment before the line
[ CODE ]   $contact=& civicrm_contact_get($params);
[/ CODE ]
to the end of the code the module works so I think that the problem is in that line.

I use civiCRM version 3.4.7 in both local and remote sites.
Some suggestions?

[Why can't past my code and there is an error "You are not allowed to post external links"??]
« Last Edit: November 07, 2011, 08:50:55 am by ilbassa »

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Check if contact exists
November 07, 2011, 09:25:55 am
Hi,

Do use the api v3, works better

In your civicrm_api ('Contact','GetSingle', array (...) )  will let you fetch one and only one contact (if >1? is will return an error

Use the api explorer to see the various options and try.
http://en.flossmanuals.net/civicrm-developer-guide/api/ should get you started

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: Check if contact exists
November 07, 2011, 10:09:29 am
Or you could use civicrm('contact', 'getcount', $params); if you want to know if it exists - wherever you found the documentation that said to use api v2 you should update it to say it's deprecated
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

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: Check if contact exists
November 08, 2011, 05:43:35 am
You should be able to use the API v3 in CiviCRM 3.4.7. Have you found the appropriate documentation?
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

ilbassa

  • I’m new here
  • *
  • Posts: 11
  • Karma: 0
  • CiviCRM version: 3.4.7
  • CMS version: Joomla 1.5
  • MySQL version: unknown
  • PHP version: 5.2.13
Re: Check if contact exists
November 08, 2011, 06:17:07 am
I've bought a module that uses the API v2 and I want to add this simple check because if the email exists the module does't work. So I try to implement it with the V2.
I'm sure that API v3 are better but this means that i had to rewrite all the code of the module i bought...

Are these codes
civicrm('contact', 'getcount', $params)
civicrm_api ('Contact','GetSingle', array (...) )
for v2 or v3?

Thanks

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Check if contact exists
November 08, 2011, 06:25:41 am
V3 (eileen meant civicrm_api ('contact','get_count').

You should be able to mix that call of the api v3 with the rest staying in api v2.

... and you should think about migrating to v3 at one point of another, won't stay supported for a lot of versions.

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: Check if contact exists
November 08, 2011, 12:44:48 pm
Nope the 'getcount' action is one word. That is a v3 option & you need to set $params['version'] => 3,

However, you can use v2 & v3 in the same codebase.

This is the first time I recall hearing someone say they bought a module (although of course lots of people have paid for various module development)
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: Check if contact exists
November 08, 2011, 01:51:49 pm
sorry, getcount indeed, meant civicrm_api ;)

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

ilbassa

  • I’m new here
  • *
  • Posts: 11
  • Karma: 0
  • CiviCRM version: 3.4.7
  • CMS version: Joomla 1.5
  • MySQL version: unknown
  • PHP version: 5.2.13
Re: Check if contact exists
November 09, 2011, 02:06:28 am
Quote from: Eileen on November 08, 2011, 12:44:48 pm
This is the first time I recall hearing someone say they bought a module (although of course lots of people have paid for various module development)

I hadn't time to spent on learning the interface to CiviCRM, so the module does what I need so I have bought it :) (for few $)

Is it enough add this lines?

require_once 'api/v3/Contact.php';
$contact = civicrm_api ('contact','get_count', $params)

and than check

if(empty($contact)){}

[with $params ["email"] = $got_email; already in the code or I have to add $params ["version"] = 3]

ilbassa

  • I’m new here
  • *
  • Posts: 11
  • Karma: 0
  • CiviCRM version: 3.4.7
  • CMS version: Joomla 1.5
  • MySQL version: unknown
  • PHP version: 5.2.13
Re: Check if contact exists
November 09, 2011, 03:55:18 am
In local site works with:

Code: [Select]
require_once 'api/v3/Contact.php';
$contact = civicrm_api('Contact','Get',array('email' => $got_email, 'version' =>3));
if(empty($contact)){}

in production site there is blank page... if i comment
Code: [Select]
$contact = civicrm_api('Contact','Get',array('email' => $got_email, 'version' =>3));
the blank page doesn't appear...

what problem could it be?

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Check if contact exists
November 09, 2011, 04:50:02 am
You don't need the require_once

You have something different between the two servers. you should check the log and/or enable display errors on the production server (there is likely an error that isn't displayed).

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

ilbassa

  • I’m new here
  • *
  • Posts: 11
  • Karma: 0
  • CiviCRM version: 3.4.7
  • CMS version: Joomla 1.5
  • MySQL version: unknown
  • PHP version: 5.2.13
Re: Check if contact exists
November 09, 2011, 05:27:05 am
Last check at the apache logs came into a 500 internal server error...

If I remove
'version' => 3
the code works but the output is
Array ( [is_error] => 1 [error_message] => Mandatory key(s) missing from params array: version )
« Last Edit: November 09, 2011, 06:48:56 am by ilbassa »

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Check if contact exists
November 10, 2011, 09:31:05 am
Quote from: ilbassa on November 09, 2011, 05:27:05 am
If I remove
'version' => 3
the code works but the output is
Array ( [is_error] => 1 [error_message] => Mandatory key(s) missing from params array: version )

Means the error is likely somewhere in api/v3/Contact.php (civicrm_api3_contact_get function)

you might want to die ("here " __LINE__"); and move that line down until you find where.

Might be easier to debug if you use the api explorer.
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

ilbassa

  • I’m new here
  • *
  • Posts: 11
  • Karma: 0
  • CiviCRM version: 3.4.7
  • CMS version: Joomla 1.5
  • MySQL version: unknown
  • PHP version: 5.2.13
Re: Check if contact exists
November 11, 2011, 02:18:11 am
I have write die("here") almost everywhere in the workflow starting from my module and going back. It is always displayed...

The die() before the last line of the return of civicrm_api3_contact_get( $params ) function in V3/Contact.php is displayed.
The die() before the "return $result;" in civicrm_api($entity, $action, $params, $extra = NULL) function in api/api.php is displayed.
I checked also some functions in v3/Utils.php

but the problem is still here:    $contact = civicrm_api('Contact', 'Get', array('email' => $got_email, 'version' => '3'));

Some other suggestion to test?

What can I do with api explorer (I found the url index2.php?option=com_civicrm&task=civicrm/ajax/doc/api#explorer)? I select "3" - "contact" - "get" - debug ok - json on and it generate a page...

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Check if contact exists
November 11, 2011, 11:51:08 am
Have you tried

 $contact = civicrm_api('Email', 'Get', array('email' => $got_email, 'version' => '3'));

ie. querying the email table rather than contact?


Are you able to turn on error_reporting so you blank screen tells you a bit more?
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

Pages: [1] 2
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Check if contact exists

This forum was archived on 2017-11-26.