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) »
  • custom fields not working on API civicrm_contact_add
Pages: [1]

Author Topic: custom fields not working on API civicrm_contact_add  (Read 1520 times)

grahamgilchrist

  • I post occasionally
  • **
  • Posts: 70
  • Karma: 3
custom fields not working on API civicrm_contact_add
December 04, 2009, 06:45:54 am
Hi all,
I have a series of custom fields on my contacts and some PHP API code on a user-side page (non civicrm) on my site. It executes during the user login (using drupal's hook_user) and updates some info in civicrm using the api.

Standard contact fields are updated with no problem, but the API is not updating my custom fields. Previously (at least version 2.8 ) this code worked fine, but I have recently upgraded the codebase to 3.0.3 and now it does not update my custom contact fields.

 I have re-checked their field IDs from the DB and they are all correct, and are of data_type "string".

This is the code I am using:
Code: [Select]
//initialise civicrm
if ( !civicrm_initialize() ) { return; }

require_once 'api/v2/Contact.php';
        drupal_set_message('<pre>'.print_r($params, true).'</pre>');
$result =& civicrm_contact_add($params);
if ( civicrm_error( $result ) ) {
$success = False;
} else {
$success = $result['contact_id'];
}

and this is the printout of the params array I am sending in (verified by the drupal log/message directly before the API call:

Code: [Select]
Array
(
    [contact_type] => Individual
    [individual_prefix] => 3
    [first_name] => Test
    [last_name] => Person
    [job_title] => Dsfsdsd
    [custom_12] =>
    [custom_13] => test13
    [custom_7] => q1
    [custom_8] => q2
    [custom_9] => q3
    [custom_10] => q4
    [contact_id] => 51723
)

Everything executes with no errors, but my custom fields are not updated, yet first name, last name and job title are.
Has anything changed in regards to how custom fields should be sent in in the latest version?
Anyone have any suggestions as to why this doesn't work?
« Last Edit: December 04, 2009, 06:47:27 am by grahamgilchrist »

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: custom fields not working on API civicrm_contact_add
December 04, 2009, 08:05:34 am
Strange.

On the edit form the names (numbers actually) of the custom_xx are the same as the ones you are trying to push ?

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

grahamgilchrist

  • I post occasionally
  • **
  • Posts: 70
  • Karma: 3
Re: custom fields not working on API civicrm_contact_add
December 04, 2009, 09:01:04 am
Hmm, well funnily enough the field id numbers are the same on the edit form, but the input id (in the HTML sense) and name attributes have extra numbers after them:
custom_7_1
custom_8_1
custom_9_1
custom_10_1
custom_67_

Is this normal? Could this be the problem? What might cause the extra numbers to be added? Should I try custom_7_1 as the fieldname in the API?
They are all normal text input fields which save as alphanumeric strings.

Could it be something to do with access control? The api is being called by a page by the anonymous user, but I was under the impression the API calls ignored ACL?
« Last Edit: December 04, 2009, 09:31:23 am by grahamgilchrist »

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: custom fields not working on API civicrm_contact_add
December 04, 2009, 09:57:09 am
No, the ids are correct.


Do you authenticate when you call the api ? I might be a permission issue indeed. Could you try to let anonymous access the custom parameters, see if it changes something ?

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

grahamgilchrist

  • I post occasionally
  • **
  • Posts: 70
  • Karma: 3
Re: custom fields not working on API civicrm_contact_add
December 04, 2009, 10:59:19 am
Thanks for the reply. I don't authenticate to the API. I have just tested giving authenticated and anonymous users the 'access all custom fields permission and this has indeed solved this.

Having looked in the API docs, I've found this page:
http://wiki.civicrm.org/confluence/display/CRMDOC/API+Overview

I have some restricted custom fields, so I don't want to give this permission out. Is there any way to authenticate to the API from a code function without giving the anonymous user these permissions?

Funny that this used to work and now doesn't. Perhaps the ACL code is more robust now and I was just taking advantage of a loophole :)

Thanks for all your help :)

P.s. something else funny I just noticed - if this is an access control issue, why can I retrieve the custom data (via civicrm_contact_get) and not set it when they both use the same permission?
« Last Edit: December 04, 2009, 11:04:05 am by grahamgilchrist »

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • custom fields not working on API civicrm_contact_add

This forum was archived on 2017-11-26.