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) »
  • Retrieving custom field values of contacts using API
Pages: [1]

Author Topic: Retrieving custom field values of contacts using API  (Read 1948 times)

xcf33

  • I post frequently
  • ***
  • Posts: 181
  • Karma: 7
  • CiviCRM version: 3.3.2
  • CMS version: Drupal 6.19/6.20
  • MySQL version: 5.x
  • PHP version: 5.2.6
Retrieving custom field values of contacts using API
October 21, 2010, 08:06:05 am
How to do it?


I looked at contact API, I can insert custom field value but not retrieve it using civicrm_contact_get, for it will only give me standard contact information.


Is there an API that will do the job???


Thanks for any help


Chang

qjensen

  • I post frequently
  • ***
  • Posts: 141
  • Karma: 11
    • Scaled Solutions
  • CiviCRM version: 4.5
  • CMS version: Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.3
Re: Retrieving custom field values of contacts using API
October 21, 2010, 01:51:14 pm
I'm not sure this can be done with the API, but you can still get the information using good ole SQL. It really isn't much more difficult than using the API.

Code: [Select]
$sql = "SELECT custom_fld_1, custom_fld_2, custom_fld_3 FROM civicrm_value_profile_custom_table WHERE entity_id = %1";
$params = array(1=>array($contact_id,'Integer'));
$dao = CRM_Core_DAO::executeQuery($sql,$params);
while ($dao->fetch) {
  $custom_val1 = $dao->custom_fld_1;
  $custom_val2 = $dao->custom_fld_2;
  $custom_val3 = $dao->custom_fld_3;
}
Quint Jensen
Web Developer
Scaled Solutions
--" Remove the 'I want you to like me' notes from your forehead and place them where they truly will do the most good -- on your mirror!" Author Unknown

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: Retrieving custom field values of contacts using API
October 21, 2010, 02:25:36 pm

this worked for me in 3.2:

Code: [Select]
require_once 'api/v2/Contact.php';

$params = array( 'contact_id' => 1,
                 "return_custom_1" => 1,
                 "return_custom_3" => 1,
                 "return_custom_2" => 1, );
$contact =& civicrm_contact_get( $params );
print_r( $contact );


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

qjensen

  • I post frequently
  • ***
  • Posts: 141
  • Karma: 11
    • Scaled Solutions
  • CiviCRM version: 4.5
  • CMS version: Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.3
Re: Retrieving custom field values of contacts using API
October 21, 2010, 03:25:50 pm
Hi Lobo,

I ran a couple of tests on my local sandbox and only get standard contact fields back, not any of the custom fields, using your sample code. In the past I have just resorted to the sql method for custom fields. Am I missing something in the process?

Thanks for clarifying.
Quint Jensen
Web Developer
Scaled Solutions
--" Remove the 'I want you to like me' notes from your forehead and place them where they truly will do the most good -- on your mirror!" Author Unknown

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: Retrieving custom field values of contacts using API
October 21, 2010, 07:45:47 pm

make sure the ID after the custom_ is the custom field ID (the id column in civicrm_custom_field) for that field

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

qjensen

  • I post frequently
  • ***
  • Posts: 141
  • Karma: 11
    • Scaled Solutions
  • CiviCRM version: 4.5
  • CMS version: Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.3
Re: Retrieving custom field values of contacts using API
October 22, 2010, 03:53:09 am
Thank you very much for explaining. I added a section to the civicrm_contact_get api docs on the wiki to explain how to do this.
Quint Jensen
Web Developer
Scaled Solutions
--" Remove the 'I want you to like me' notes from your forehead and place them where they truly will do the most good -- on your mirror!" Author Unknown

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Retrieving custom field values of contacts using API

This forum was archived on 2017-11-26.