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) »
  • Discussion (deprecated) »
  • Alpha and Beta Release Testing »
  • 2.1 Release Testing »
  • Adding custom field data using the API
Pages: [1]

Author Topic: Adding custom field data using the API  (Read 7009 times)

drob

  • Guest
Adding custom field data using the API
September 08, 2008, 11:23:30 pm
I'm using the latest 2.1 on a LAMP server (Dreamhost) with MySQL 5 etc and Drupal 6.x.  I am attempting to use the API to create contact records from within Drupal.  I have the following:

Code: [Select]
       
// this is the relevant code from within another procedure
// find the record in civicrm via email
$applicant = array(
        'email' => $email,
        );                                                                                                                                     
        $contacts = &civicrm_contact_search($applicant);
        // make an assumption
        $contact = $contacts[0];   
        if ( !$contact || !isset($contact->id)) {                                                                                                                               
            $params = array('first_name' => $first_name,                                                                                                         
                            'last_name'  => $last_name,                                                                                                           
                            'email'      => $email,                                                                                                               
                            'contact_type'  => 'Individual',
                            'custom_4' => $party,
                            'custom_5' => $first_time,
                            'custom_6' => $state,
            );         
            $contact = &civicrm_contact_add($params);                                                                                                               
        }                                                                                                                                                                       
        $params = array(
                        'contact_id.1' => $contact['contact_id'],
                        'group_id'     => 2 );
        civicrm_group_contact_add($params);
}                                                                 

So I get the contact record in CiviCRM, the first, last names and email address are good.  The contact is added to the right group, however the custom fields are not getting populated.  I've triple checked the field names ("custom_5") etc.  Help.

Thanks,

Dan

Kurund Jalmi

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4169
  • Karma: 128
    • CiviCRM
  • CiviCRM version: 4.x, future
  • CMS version: Drupal 7, Joomla 3.x
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Adding custom field data using the API
September 09, 2008, 12:11:36 am
Can you check http://forum.civicrm.org/index.php/topic,2967.0.html ...

Testcase for adding Contact with custom data:

http://fisheye.civicrm.org/browse/CiviCRM/branches/v2.0/test-new/SimpleTest/api-v2/ContactAddWithCustomData.php?r=13903

Also make sure you are passing right values for custom data

HTH

Kurund
« Last Edit: September 09, 2008, 01:06:34 am by Kurund Jalmi »
Found this reply helpful? Support CiviCRM

drob

  • Guest
Re: Adding custom field data using the API
September 09, 2008, 12:57:51 pm
Ok - I've read through that thread - tried a couple of things - no joy yet.  I am trying to figure out how to run the test but no luck yet.  This page has bad links - http://wiki.civicrm.org/confluence/display/CRM/Writing+SimpleTest+unit+tests.  Help appreciated.

Kurund Jalmi

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4169
  • Karma: 128
    • CiviCRM
  • CiviCRM version: 4.x, future
  • CMS version: Drupal 7, Joomla 3.x
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Adding custom field data using the API
September 09, 2008, 02:14:58 pm
You can check this code snippet, simpletest is currently broken.
Quote
$params = array ('first_name'    => 'hello',
                 'last_name'     => 'hi',
                 'contact_type'  => 'Individual',
                 'phone'         => '123456',
                 'phone_type'    => 'Phone',
                 'email'         => 'hello@hi.com',
                 'prefix'        => 'Mr',
                 'suffix'        => 'VII',
                 'gender'        => 'Male',
                 'do_not_trade'  => 1,
                 'preferred_communication_method' => array(
                                                           '2' => 1,
                                                           '3' => 1,
                                                           '4' => 1,
                                                           ),
                 "custom_6" => '10',
                 "custom_7" => 'this is test notes',
                 "custom_8" => '100',
                 "custom_9" => Array (
                                      "M" => 8,
                                      "d" => 9,
                                      "Y" => 2008
                                      ),
                 
                 "custom_10" => 1,
                 "custom_11" => 1000,
                 "custom_12" => 1101,
                 "custom_13" => "http://dddd.blogspot.com"
                 );

$addedContact = &civicrm_contact_add( $params );


Above code is adding contact with custom data.

HTH

Kurund
Found this reply helpful? Support CiviCRM

drob

  • Guest
Re: Adding custom field data using the API
September 11, 2008, 08:41:36 am
Still no joy.

I tried code that lobo provided - http://paste2.org/p/71853.  Here's the result:

Code: [Select]
Array
(
    [contact_id] => 2
    [is_error] => 0
)

The contact is created, the custom fields are not populated.

In CiviCRM i have three fields State, Party, First Time.  I'm assuming the ID is in the URL "/custom/group/field?action=update&reset=1&gid=1&id=1" (this is for State)  So I'm using the following code (very similar to what Lobo provided)

Code: [Select]

           $params = array('first_name' => $first_name,                                                                                                         
                            'last_name'  => $last_name,                                                                                                           
                            'email'      => $email,                                                                                                               
                        'contact_type'  => 'Individual',
                        'custom_1' => 'CA',
            'custom_2' => 'Democratic',
            'custom_3' => 'yes',
                            );         

in this test I have strings - in my production code they will be string variables.  The fields are all "alphanumeric" and searchable.  The contacts are being added - but not the custom data.  I am using CiviCRM code from here: http://voxel.dl.sourceforge.net/sourceforge/civicrm/civicrm-2.1.beta4-drupal.tar.gz. 

It feels like either we're using different code or I've got some simple syntax error (wrong field names??)  In the table civicrm_custom_field the custom_group_id's are: 1, 2, 3 respectively.  The column names are: state_1, party_2, first_time_3.  I've stepped through the debugger - I didn't go deep enough tell where the actual values are put in a sql statement and sent to the db - but I did verify that the value array contains what I think are the right labels and values. 

Thanks,

Dan


Kurund Jalmi

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4169
  • Karma: 128
    • CiviCRM
  • CiviCRM version: 4.x, future
  • CMS version: Drupal 7, Joomla 3.x
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Adding custom field data using the API
September 11, 2008, 08:48:47 am
Can you give more details on custom fields, what type of fields etc.

Kurund
Found this reply helpful? Support CiviCRM

drob

  • Guest
Re: Adding custom field data using the API
September 11, 2008, 09:03:06 am
In CiviCRM i have three fields "State", "Party", "First Time". They are all defined as Alphanumeric fields and are "Searchable".  They are not "Required".  The URL for State is: "/custom/group/field?action=update&reset=1&gid=1&id=1" I'm assuming the ID is in the URL and in this case is "1" - the others are "2" and "3" respectively.  I'm not sure what else I can tell you.

drob

  • Guest
Re: Adding custom field data using the API
September 11, 2008, 11:17:38 am
Ok - Lobo took a look and figured out it was a caching problem.  I tried to enter a ticket twice but got -

HTTP Status 404 - No view for result [error] exists for action [ViewIssue]

Here is the body of the ticket:

I'm using 2.1 b4 with drupal 6.x

Couldn't get the api to update custom fields.

steps:

- logged in to drupal as admin
- navigated to add new custom field group
- added it
- added three fields - all alphanumeric, all searchable
- looked up the id numbers in the URL
- executed this code:

Code: [Select]
<?php
$params 
= array( 'contact_type' => 'Individual',
                 
'first_name'   => 'Foo',
                 
'last_name'    => 'Fighter',
                 
'custom_4'     => 'Edu',);
require_once 
'../civicrm.config.php';
require_once 
'api/v2/Contact.php';
$result = civicrm_contact_add( $params );
CRM_Core_Error::debug( $result );
?>


- Contact was added
- custom field was not populated
- truncated civicrm_cache
- ran code again
- contact was added and field updated

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Adding custom field data using the API
September 11, 2008, 11:24:26 am

the issue number is: http://issues.civicrm.org/jira/browse/CRM-3533, if u want to track it. Hopefully we can reproduce and hence fix it :)

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Discussion (deprecated) »
  • Alpha and Beta Release Testing »
  • 2.1 Release Testing »
  • Adding custom field data using the API

This forum was archived on 2017-11-26.