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) »
  • Contact API, problems adding or updating custom data
Pages: [1]

Author Topic: Contact API, problems adding or updating custom data  (Read 1936 times)

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
Contact API, problems adding or updating custom data
August 16, 2010, 05:35:49 am
I am having trouble updating custom data with the Contact API function civicrm_contact_create or civicrm_contact_update.
My testcode is the following:
Code: [Select]
$persoon = array(
    "contact_id"    =>  276,
    "contact_type"  =>  "Individual",
    "first_name"    =>  "Charlotte",
    "last_name"     =>  "Hommel",
    "custom_2"      =>  "3456",
    "custom_1"      =>  "H",
    "custom_3"      =>  "456789012");

$result = &civicrm_contact_update($persoon);

There is a field with ID 1, 2 and 3 in the table civicrm_custom_field. There is no update of the fields, and the result parameter contains error 0.
When I check the code, I eventually end up in the CRM/Contact/BAO/Contact.php. In the function create there is a bit of code dealing with the custom data:
Code: [Select]
       if ( CRM_Utils_Array::value( 'custom', $params ) &&
             is_array( $params['custom'] ) ) {
            require_once 'CRM/Core/BAO/CustomValueTable.php';
            CRM_Core_BAO_CustomValueTable::store( $params['custom'], 'civicrm_contact', $contact->id );
        }

If I check, I have the following values in my $params:

Array ( [contact_id] => 276 [contact_type] => Individual [first_name] => Charlotte [last_name] => Hommel [custom_2] => 3456 [custom_1] => H [custom_3] => 456789012 [custom] => Array ( ) [is_deceased] => [website] => )

It seems as if the CRM_Core_BAO_CustomValueTable::store is never executed though, and even if it was, the $params['custom'] array is empty? I am lost....

Erik
« Last Edit: August 16, 2010, 10:47:41 am by Erik Hommel »
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

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: Contact API, problems adding or updating custom data
August 17, 2010, 12:56:14 am
For the moment I have solved my issue because I bypass the API. I use the CRM_Core_BAO_CustomValueTable::setValues function because I know the Custom Data. This does work.
Code: [Select]
$test2 = array(
    "entityID"  =>  276,
    "custom_1"  =>  "H",
    "custom_2"  =>  "23456",
    "custom_3"  =>  "456789012"
);
$test3 = CRM_Core_BAO_CustomValueTable::setValues($test2);

I would like to fix the API though....so I would be grateful for an answer to the post even if the issue itself is not project-threatening :-)
Erik
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Contact API, problems adding or updating custom data
August 19, 2010, 08:12:34 am
Hi,

Haven't used it, don't know for sure. Could you have a look in the test see if the custom fields are covered ?

Otherwise, yes, it should work, if it doesn't it should be fixed. Not sure what's the best way, but #civicrm is your friend.

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

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: Contact API, problems adding or updating custom data
August 19, 2010, 09:10:48 am
Hmmmm I should have been so lucky :-)
I will check the code and see if I can fix it, and get on the IRC!
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

chirayu

  • Guest
Re: Contact API, problems adding or updating custom data
September 03, 2010, 05:54:53 am
Erik is it true that the fields you were trying to add data for were set as 'View Only' when you created them??

Chirayu

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: Contact API, problems adding or updating custom data
September 05, 2010, 11:28:50 pm
Sorry for the late answer, I was away on school camp :-)
And yes, they were set to 'view only', I will test with the 'view only' ticked off and see if it works then. I hope that makes no difference though, I have always interpreted the 'view only' as not to be changed by the user and assumed I could change it with a script. That is what I need, values need to be changed automatically because the data is synchronized between two databases, and the user should not be able to change it. Will report back on the results.
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: Contact API, problems adding or updating custom data
September 06, 2010, 12:23:45 am
Erik - Have you checked whether the custom data is in the table or are you only looking from the GUI? If they should be surrounded by separators but aren't then you won't see them though the front end

This is the snipped of code I'm using in civimigrate (https://svn.fuzion.co.nz/repos/fuzion-code/trunk/drupal/modules/custom/civimigrate to deal with this)
 
Code: [Select]
  //TODO convert custom fields into arrays using separator string before they get this far
  require_once 'CRM/Core/BAO/CustomField.php';
  foreach ($custom as $customFieldNumber => $customFieldName){
  $customFormatted = array();
  //FIXME I need to identify custom field type - at the moment only doing Individual
  CRM_Core_BAO_CustomField::formatCustomField($customFieldNumber, $customFormatted,  array($params[ $customFieldName]=>1),
                                       'Individual',  null,
                                       true);
                                       
 
  $params['custom_' . $customFieldNumber] =  $customFormatted[$customFieldNumber][-1]['value'];                                 
 
//
  }
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: Contact API, problems adding or updating custom data
September 06, 2010, 12:29:31 am
Hi Eileen,
I was checking in both the GUI and the database, will have a go with your code, thanks!
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Contact API, problems adding or updating custom data

This forum was archived on 2017-11-26.