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 (Moderator: Donald Lobo) »
  • Updating custom data via API
Pages: [1]

Author Topic: Updating custom data via API  (Read 4074 times)

Chris Burgess

  • Ask me questions
  • ****
  • Posts: 675
  • Karma: 59
Updating custom data via API
April 04, 2008, 04:30:54 am
A few similar threads have been raised on this, but I can't see the answer in them yet.

In 1.8 / 1.9, we were able to write to custom data on a contact using crm_create_custom_value().

CRM-2691 required the removal of the custom data functions because they are deprecated and have been replaced by v2 versions, but I can't find a documented method for achieving the same thing in v2.

It's possible to find custom data by civicrm_contact_search() if you add the correct params to return your additional custom values. Eg,
Code: [Select]
$params = array( 'contact_id' => 6435, 'return.custom_88' => 1 ) ;
$res = civicrm_contact_search($params);

However, it doesn't seem that you can set the custom data using a similar call to civicrm_contact_add() - can you?
@xurizaemon ● www.fuzion.co.nz

Manish Zope

  • I’m new here
  • *
  • Posts: 18
  • Karma: 4
Re: Updating custom data via API
April 04, 2008, 07:17:07 am
civicrm_contact_add($params) api was having some bug (due to which custom data was not set)
it is fixed with r13868

The changelog :
http://fisheye.civicrm.org/browse/CiviCRM/branches/v2.0/api/v2/Contact.php?r1=13681&r2=13868
http://fisheye.civicrm.org/browse/CiviCRM/branches/v2.0/api/v2/utils.php?r1=13584&r2=13868

Check this testcase
It uses Contact v2 API to set custom data for a Contact
http://svn.civicrm.org/civicrm/branches/v2.0/test-new/SimpleTest/api-v2/ContactAddWithCustomData.php

HTH
Manish

Chris Burgess

  • Ask me questions
  • ****
  • Posts: 675
  • Karma: 59
Re: Updating custom data via API
April 04, 2008, 03:06:01 pm
Thanks Manish, that does work for creating new contacts, but the same approach for updating contacts doesn't seem to function as expected.

As I understand it, in v2 we should use civicrm_contact_add() for both adds and updates - is that right?

Here's my test case trying to make the same function work for update - it fails with duplicate entry errors when trying to insert where the value already exists.

I've modified the custom_XX entries to suit our database, you'll probably need to change them back :)

(I removed the code sample here and posted a new copy which demonstrates the same error below)
« Last Edit: April 08, 2008, 01:59:24 am by xurizaemon »
@xurizaemon ● www.fuzion.co.nz

Chris Burgess

  • Ask me questions
  • ****
  • Posts: 675
  • Karma: 59
Re: Updating custom data via API
April 08, 2008, 02:05:36 am
Manish, this sample should demonstrate the error I'm reporting.

The test case below passes with lines 31 and 59 commented, and demonstrates an SQL error trying to INSERT custom data rather than UPDATE when those lines are uncommented. There was an error in the earlier version I posted, but this copy should demonstrate the issue properly.

With lines 31 / 60 commented, it works fine, and the contact is created with email "man7" then updated with email "man7+custom".

However, with them uncommented as in the sample below, it fails with an error like,

Quote
INSERT INTO custom_value_1_Geography  SET `pods` = 'Different information for custom field of type alphanumeric - text', domain_id = 1, entity_id = 18446  [nativecode=1062 ** Duplicate entry '1-18446' for key 2]

Code: [Select]
<?php

require_once 'api/v2/Contact.php';

class 
TestOfContactAddAndUpdateWithCustomDataAPIV2 extends CiviUnitTestCase 
{
    function 
testCreateIndividualwithAll() 
    {
        
// Create contact
        
$params = array('first_name'    => 'abc' . time(),
                        
'last_name'     => 'xyz' . time(), 
                        
'contact_type'  => 'Individual',
                        
'phone'         => '999999',
                        
'phone_type'    => 'Phone',
                        
'email'         => 'man7@yahoo.com',
                        
'do_not_trade'  => 1,
                        
'preferred_communication_method' => array(
                                                                  
'2' => 1,
                                                                  
'3' => 1,
                                                                  
'4' => 1,
                                                                  ),
                        
'custom_88'      => '100000000',
                        
'custom_87'      => 'Information for custom field of type alphanumeric - text'
                        
);

        
$contact =& civicrm_contact_add($params);

        
$this->assertNotNull( $contact['contact_id'] );
                
// now update the contact with a different set of custom_ values - comment this line to demonstrate that test case works if not updating custom data
      
$oParams['custom_87']  = 'Different information for custom field of type alphanumeric - text' ;
        
$oParams['email']        = 'man7+custom@yahoo.com' ;
        
$oParams['phone']        = '123-4567' ;
        
$oParams['phone_type']   = 'Phone' ;
        
$oParams['contact_id']   = $contact['contact_id'] ;
        
$oParams['contact_type'] = 'Individual' ;
        
$newEmail                = $oParams['email'] ;

        
$contact =& civicrm_contact_add($oParams) ;
        
        
// Get the contact values
        
$retrieve = array( 'contact_id'        => $contact['contact_id'],
                           
'return.first_name' => 1,
                           
'return.last_name'  => 1,
                           
'return.phone'      => 1,
                           
'return.email'      => 1,
                           
'return.custom_88'  => 1,
                           
'return.custom_87'  => 1
                           
);

        
$getContact = civicrm_contact_get( $retrieve );
                
        
$this->dump( array( $getContact ) );

        
$this->assertEqual( $getContact['first_name'],  $params['first_name'] );
        
$this->assertEqual( $getContact['last_name'],   $params['last_name']  );
        
$this->assertEqual( $getContact['email'],       $newEmail   );

// comment this line to demonstrate test case working if not updating custom data
        
$this->assertEqual( $getContact['custom_87'],   $oParams['custom_87']   );
        
$this->assertEqual( $getContact['custom_5'],    $params['custom_5']   );
    }
}
« Last Edit: April 08, 2008, 02:07:42 am by xurizaemon »
@xurizaemon ● www.fuzion.co.nz

Chris Burgess

  • Ask me questions
  • ****
  • Posts: 675
  • Karma: 59
Re: Updating custom data via API
April 08, 2008, 04:35:32 pm
http://issues.civicrm.org/jira/browse/CRM-2962

I updated the SimpleTest to match the updated version in SVN. Then I re-added some code at lines 111 ~ 122 which demonstrates the error if civicrm_contact_add() is asked to update a contact and given custom data.

The updated SimpleTest is attached here - please try it.
« Last Edit: April 08, 2008, 06:35:07 pm by xurizaemon »
@xurizaemon ● www.fuzion.co.nz

Manish Zope

  • I’m new here
  • *
  • Posts: 18
  • Karma: 4
Re: Updating custom data via API
April 09, 2008, 12:00:54 am
fixed the custom data update bug in r13903 (http://issues.civicrm.org/jira/browse/CRM-2962)

changelog :
http://fisheye.civicrm.org/browse/CiviCRM/branches/v2.0/api/v2/Contact.php?r1=13868&r2=13903
http://fisheye.civicrm.org/browse/CiviCRM/branches/v2.0/api/v2/utils.php?r1=13868&r2=13903

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

Manish

ijames

  • Guest
Re: Updating custom data via API
April 09, 2008, 01:02:32 am
I have upgraded both Contact.php and utils.php. 

But, from what I can tell from the test case, this doesn't address the changing of a custom field does it?

I'm trying to do:

$params = array(
   'contact_id' => $contact_id,
   'custom_4'   => $results['stassembly'],
   'custom_3'    => $results['stsenate'],
);
civicrm_contact_add($params);


And it's failing.  I've checked the values before and after and they both return this:

Array
(
    [contact_id] => 4377
    [civicrm_value_1_Political_Data_id] => 81
    [custom_3] => 11
    [custom_4] => 4
)


Which is what they were before the modification...

Is this still an unaddressed bug or is there something I'm missing.  I've not changed any other files since 2.0.1 mid February. 

Thanks much for the assistance!

James

Manish Zope

  • I’m new here
  • *
  • Posts: 18
  • Karma: 4
Re: Updating custom data via API
April 09, 2008, 08:54:27 am
James,

In your params array, contact_type is missing
This is the reason why values are not getting updated

( This was the bug in civicrm_contact_add api .. which is fixed with r13921
  Check the changelog)

ijames

  • Guest
Re: Updating custom data via API
April 09, 2008, 11:17:53 am
Manish,

That was indeed the problem!

Thanks much!

James



Summary:

For setting custom fields:
  • Use civicrm_contact_add()
  • Include the 'contact_id' and the 'contact_type' in the parameter array
  • Include the custom_x fields that are to be updated or included.

E.g.

$params = array('contact_id'=$mycontact,'contact_type'=>'Individual','custom_1'=>'happy');
civicrm_contact_add($params);
$params = array('contact_id'=$mycontact,'return.custom_1'=>1);
print_r(civicrm_contact_get($params));


Should return an array of contact_id and 'happy' right?

Right?

dougall

  • Guest
Re: Updating custom data via API
April 10, 2008, 04:24:24 am
hi,

excellent ... this works great for custom fields on Individual contacts.

however, is there a way of doing something similar for custom fields on groups?

civicrm_groups_get() appears to return an array of group details for groups which fulfil the params, but this doesn't seem to include their associated custom fields.

maybe i'm  going about this the wrong way?

thanks ... d

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Updating custom data via API

This forum was archived on 2017-11-26.