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) »
  • Support »
  • Using CiviCRM (Moderator: Dave Greenberg) »
  • using civicrm_contact_add()
Pages: [1] 2

Author Topic: using civicrm_contact_add()  (Read 5445 times)

mtkoan

  • Guest
using civicrm_contact_add()
February 17, 2010, 11:52:31 pm
Hello all,

I am trying to edit the drupal module "webform" so it adds data to the civiCRM sql tables.  For this I am using the api function civicrm_contact_add().  My problem is I don't know how to (include?) use API.

I have something like this in my "webform_submissions.inc" in my modules directory:
Code: [Select]
civicrm_initialize(true);
....
civicrm_contact_add($contactParams)

And get the error:

Fatal error: Call to undefined function civcrm_initialize() in /home/<stuff>/sites/all/modules/webform/webform_submissions.inc on line 44

Can anyone help?

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: using civicrm_contact_add()
February 18, 2010, 12:18:17 am
Hi,
there is more information to be found on the API's here: http://wiki.civicrm.org/confluence/display/CRMDOC/Contact+APIs

If you want to run your script outside of CiviCRM, you will need to initialize CiviCRM first. You can do that like this:
Code: [Select]
/* Initialize CiviCRM so that API's can be used */
require_once 'CRM/Core/Config.php';
$config =& CRM_Core_Config::singleton( );

You will also need to include the API you want to use:
Code: [Select]
/* Load the required CiviCRM API's */
require_once('api/v2/Contact.php');
Good luck!
Erik
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: using civicrm_contact_add()
February 18, 2010, 12:25:55 am
Did you copy & paste this? If so you have a typo in civicrm_initialize

Code: [Select]
Fatal error: Call to undefined function civcrm_initialize() in /home/<stuff>/sites/all/modules/webform/webform_submissions.inc on line 44
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

mtkoan

  • Guest
Re: using civicrm_contact_add()
February 18, 2010, 06:35:05 am
Thanks for the great info.  :)

I'll try that out as soon as I can.

Edit: So, tried suggested.
Now this error:

Code: [Select]
You need to define CIVICRM_UF in civicrm.settings.php
Seems like the environment isn't set up correctly at all, and I have no idea how to do so.
« Last Edit: February 18, 2010, 11:14:11 am by mtkoan »

mtkoan

  • Guest
Re: using civicrm_contact_add()
February 18, 2010, 12:58:08 pm
This is what worked:

Edited include_path in php.ini.

then

Code: [Select]
civicrm_initialize(true);
require_once('api/v2/Contact.php);

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: using civicrm_contact_add()
February 19, 2010, 10:16:00 am
Glad you got it solved!
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

jday

  • I post occasionally
  • **
  • Posts: 62
  • Karma: 6
  • CiviCRM version: 4.2
  • CMS version: 7.15
Re: using civicrm_contact_add()
March 22, 2010, 12:56:07 am
I am searching for a way to create a contact record from web inquiries (contact us) page, do you really need to modify the php.ini file to do this? I like the use of the webform module idea.

mtkoan can you elaborate on what you did?

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: using civicrm_contact_add()
March 22, 2010, 01:32:54 am
You could just use a CiviCRM profile like this

http://www.circus.org.nz/civicrm/profile/create?reset=1&gid=13

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

jday

  • I post occasionally
  • **
  • Posts: 62
  • Karma: 6
  • CiviCRM version: 4.2
  • CMS version: 7.15
Re: using civicrm_contact_add()
March 22, 2010, 01:08:33 pm
yes I tried that but then the 'message' is in the contact record and if the user submits a second 'contact us' form then it overwrites the previous message so the 1st message is lost. And the people who need to respond to the messages then have to go to the contact records to find them.

I've set up a content-type of 'inquiry' in drupal and a view that will list all inquiries and will sort by status, region and subject. Now I wonder if I can set up a rule with Rules, to run the civicrm_contact_add() each time an inquiry is submitted using the 'execute custom php' action.

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: using civicrm_contact_add()
March 22, 2010, 01:29:24 pm
Yes, it would be great to be able to use an activity field on a contact profile
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

jday

  • I post occasionally
  • **
  • Posts: 62
  • Karma: 6
  • CiviCRM version: 4.2
  • CMS version: 7.15
Re: using civicrm_contact_add()
March 22, 2010, 03:04:54 pm
This is the code I am trying to use in Rules as 'execute custom php' action

Code: [Select]
require_once($_SERVER['DOCUMENT_ROOT'].
'/sites/default/civicrm.settings.php');

/* Initialize CiviCRM so that API's can be used */
require_once ('$_SERVER['DOCUMENT_ROOT']./sites/all/modules/civicrm/CRM/Core/Config.php';
$config =& CRM_Core_Config::singleton( );

/* Load the required CiviCRM API's */
require_once('$_SERVER['DOCUMENT_ROOT']./sites/all/modules/civicrm/api/v2/Contact.php');

    $params = array(
                    'first_name'    => $node->field_fname[0]['value'],
                    'last_name'     => $node->field_lname[0]['value'],
                    'email'            => $node->field_contact_email[0]['value'],
                    'contact_type'  => 'Individual',
    'dupe_check'   => 'true',

                    );
    $contact =&civicrm_contact_add( $params );
    print_r($contact);


I've also tried it with the token values but either way it does not add the contact to civicrm, can any one point out what I'm doing wrong?

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: using civicrm_contact_add()
March 23, 2010, 12:46:59 am
Do you get a message returned from the civicrm_contact_add function?
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

jday

  • I post occasionally
  • **
  • Posts: 62
  • Karma: 6
  • CiviCRM version: 4.2
  • CMS version: 7.15
Re: using civicrm_contact_add()
March 23, 2010, 02:29:16 pm
no message, no errors, seems like nothing happens

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: using civicrm_contact_add()
March 24, 2010, 12:03:34 am
That is strange....there are no return values in $contact whatsoever? I once had trouble with the 'dupe' rules but that did give me messages.....I will have a go with your code later on the day.
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

jday

  • I post occasionally
  • **
  • Posts: 62
  • Karma: 6
  • CiviCRM version: 4.2
  • CMS version: 7.15
Re: using civicrm_contact_add()
April 14, 2010, 07:32:39 pm
If anyone is interested I was able to use this code to insert a new contact record with the rules module, unfortunately there is no check for a duplicate match. I couldn't get the civicrm_contact_add() function to work so this is my plan B that works. I have a node type with cck name and address fields. Then I made a rule that activates on new node saved, this is in the 'execute custom php':

Code: [Select]
$firstname = $node->field_fname[0]['value'];
$lastname = $node->field_lname[0]['value'];
$email = $node->field_contact_email[0]['email'];
$city = $node->field_city[0]['value'];
$state = $node->field_state[0]['value'];
$postalcode = $node->field_zip[0]['value'];
if ($firstname && $lastname) {
   $sortname = $lastname.', '.$firstname;
}
else {
   $sortname = $email;
}

db_set_active('civicrm');
 
//insert name into civicrm
db_query("INSERT INTO civicrm_contact (first_name, last_name, sort_name, display_name, contact_type)
VALUES ('$firstname', '$lastname', '$sortname', '$firstname $lastname', 'Individual')");
$id = mysql_insert_id();

db_query("INSERT INTO civicrm_email (contact_id, email, is_primary) values ('$id', '$email', '1')");
db_query("INSERT INTO civicrm_address (contact_id, postal_code, city, is_primary, state_province_id) values ( '$id', '$postalcode', '$city','1', '$state')");

db_set_active('default');
« Last Edit: April 30, 2010, 12:10:48 am by jday »

Pages: [1] 2
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM (Moderator: Dave Greenberg) »
  • using civicrm_contact_add()

This forum was archived on 2017-11-26.