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) »
  • 'How to use custom data in the api' documentation
Pages: [1] 2

Author Topic: 'How to use custom data in the api' documentation  (Read 4582 times)

Michael McAndrew

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1274
  • Karma: 55
    • Third Sector Design
  • CiviCRM version: various
  • CMS version: Nearly always Drupal
  • MySQL version: 5.5
  • PHP version: 5.3
'How to use custom data in the api' documentation
October 07, 2011, 04:38:51 am
hey there,

is there a page where using the api with custom data is documented?

i have a feeling there isn't and am volunteering to write it, if someone can give me the recommended way to do it these days.

how is it meant to interact with the getfields api call?

do you have to know the id of the field and use

if you have some examples of it is use or could point me to cool / simple ways of working with it, that would be cool

ta!
michael
« Last Edit: November 21, 2011, 02:53:36 am by michaelmcandrew »
Service providers: Grow your business, build your reputation and support CiviCRM. Become a partner today

demeritcowboy

  • Ask me questions
  • ****
  • Posts: 570
  • Karma: 42
  • CiviCRM version: Always the latest!
  • CMS version: Drupal 6 mostly, still evaluating 7.
  • MySQL version: Mix of 5.0 / 5.1 / 5.5
  • PHP version: 5.3, usually on Windows
Re: custom data for entitied api docs are scarce
October 07, 2011, 06:25:00 am
Look at api/v3/examples/ActivityCreate and ActivityGet

There may be more doc elsewhere - not sure.

Michael McAndrew

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1274
  • Karma: 55
    • Third Sector Design
  • CiviCRM version: various
  • CMS version: Nearly always Drupal
  • MySQL version: 5.5
  • PHP version: 5.3
Re: custom data for entitied api docs are scarce
October 10, 2011, 11:14:42 am
thanks for that.  i guess that is all you need.  i was thinking that api3 would allow you to do cool stuff like easily return custom data fields without having to know the custom_# id.  Something like custom.set_name.field_name but maybe that is something for api4 :)
Service providers: Grow your business, build your reputation and support CiviCRM. Become a partner today

Michael McAndrew

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1274
  • Karma: 55
    • Third Sector Design
  • CiviCRM version: various
  • CMS version: Nearly always Drupal
  • MySQL version: 5.5
  • PHP version: 5.3
Re: custom data for entitied api docs are scarce
October 10, 2011, 11:30:49 am
ps. never the less i added this to the api wiki page http://wiki.civicrm.org/confluence/display/CRMDOC40/CiviCRM+Public+APIs
Service providers: Grow your business, build your reputation and support CiviCRM. Become a partner today

Michael McAndrew

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1274
  • Karma: 55
    • Third Sector Design
  • CiviCRM version: various
  • CMS version: Nearly always Drupal
  • MySQL version: 5.5
  • PHP version: 5.3
Re: How to use custom data in the api documentation
November 21, 2011, 02:53:16 am
Hey there,

I updated the page today with the two different ways you can use custom data and the API.  I am not sure if one of those methods is being deprecapted in favour of the other.  Anyone able to shed light?

Michael
Service providers: Grow your business, build your reputation and support CiviCRM. Become a partner today

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: 'How to use custom data in the api' documentation
November 23, 2011, 06:11:30 pm
CustomValue does take other formats - although default is custom_x & I never really got my head around the others - in code comments help a bit on this.

http://api.civicrm.org/v3/CiviCRM_APIv3/API_CustomField/_api---v3---CustomValue.php.html#functioncivicrm_api3_custom_value_create
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

chriscant

  • I post occasionally
  • **
  • Posts: 66
  • Karma: 4
    • PHDCC
  • CiviCRM version: 4.2.19
Re: 'How to use custom data in the api' documentation
November 29, 2011, 07:10:10 am
This is how I use the basic API v3 to get the full contact info:

$params = array( 'contact_id' => $cid, 'version' => 3, );
$retrieved = civicrm_api('Contact','Get',$params);


I have a custom data set called "Host availability", with fields "Availability", "End Date" and "Vacancy".  Here's how to get these values:

$params = array( 'entity_id' => $cid, 'version' => 3,
  'entity_table' => '',
  'return.host_availability:availability' => 1,
  'return.host_availability:end_date' => 1,
  'return.host_availability:vacancy' => 1,
  'sequential' => 1,
  );
$fields = civicrm_api('CustomValue','Get',$params);
if( $fields['is_error'] == 0)
{
  $Availability = $fields['values'][0][0];
  $strEndDate = $fields['values'][1][0];
  $IsVacancy = $fields['values'][2][0];
}


Note the use of "sequential".  If you don't use this, then you get results such as $fields['values'][133][0] where 133 is the custom_133 value that you are trying to avoid.

The mapping between custom data set/fields names and the machine name to use in $params isn't necessarily as easy as hinted at above.  For precise details, look in tables civicrm_custom_group and civicrm_custom_field.

If you get the field element wrong then all the all fields in the data set are returned (with error in Drupal log), eg:
  'return.host_availability:wrong' => 1,

The empty 'entity_table' value avoids a warning being written to the Drupal log.
« Last Edit: November 30, 2011, 10:38:17 am by chriscant »

chriscant

  • I post occasionally
  • **
  • Posts: 66
  • Karma: 4
    • PHDCC
  • CiviCRM version: 4.2.19
Re: 'How to use custom data in the api' documentation
November 29, 2011, 09:05:36 am
Further to my last post, it seems that /api/v3/CustomValue.php causes a warning in the Drupal log in civicrm_api3_custom_value_get() around line 171:
list($c, $i, $n) = explode('_', $id);

In my case, $id was something like "custom_133" which will only explode into two elements.
A potential fix this:

list($c, $i, $n) = array_pad(explode('_', $id),3,0);

chriscant

  • I post occasionally
  • **
  • Posts: 66
  • Karma: 4
    • PHDCC
  • CiviCRM version: 4.2.19
Re: 'How to use custom data in the api' documentation
August 12, 2012, 04:31:03 am
To set or update a custom value, this works:

$params = array(
   'version' => 3,
   'entity_id' => $cid,
   'custom_133' => 'value',
);
civicrm_api( 'custom_value','create',$params );

Luciano S.

  • I post occasionally
  • **
  • Posts: 83
  • Karma: 2
  • iXiam Team Leader
  • CiviCRM version: 4.2+ / 4.3+ / 4.4+
  • CMS version: Drupal
  • MySQL version: 5.1+ / 5.5+
  • PHP version: 5.3+ / 5.4+
Re: 'How to use custom data in the api' documentation
August 22, 2012, 07:18:32 am
Following the end of this thread I attach a patch file to avoid PHP Notice messages in CustomValue create method.
I haven't worked with the get method but it looks like it has the same Notice issues, in the explode() calls when the parameter has not included "_" or ":" symbol.

I don't know if it worth it to create Jira bug for this, when there is only Notice msg easily avoidable by configuration but very  annoying in dev staging when I have all the PHP alerts ON :)

cheers


Michael McAndrew

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1274
  • Karma: 55
    • Third Sector Design
  • CiviCRM version: various
  • CMS version: Nearly always Drupal
  • MySQL version: 5.5
  • PHP version: 5.3
Re: 'How to use custom data in the api' documentation
August 24, 2012, 05:45:08 am
Hey there,

I would definitley file an issue patch for that for the reasons that you mention below. always good to have the code clean and shiny :)

Michael
Service providers: Grow your business, build your reputation and support CiviCRM. Become a partner today

Luciano S.

  • I post occasionally
  • **
  • Posts: 83
  • Karma: 2
  • iXiam Team Leader
  • CiviCRM version: 4.2+ / 4.3+ / 4.4+
  • CMS version: Drupal
  • MySQL version: 5.1+ / 5.5+
  • PHP version: 5.3+ / 5.4+
Re: 'How to use custom data in the api' documentation
October 25, 2012, 04:36:29 am
better later than never :)
JIRA issue created and patch attached.

http://issues.civicrm.org/jira/browse/CRM-11151

cheers!

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: 'How to use custom data in the api' documentation
October 25, 2012, 01:10:57 pm
karma+

(my personal hatred is the one letter variable names - still I imagine the person who put them there hates my over-long-superdescriptive variable names - perhaps having autocomplete shades my judgement here)
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

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: 'How to use custom data in the api' documentation
October 25, 2012, 09:03:36 pm
Aw come on Eileen, one letter variables, they're so cute.
Try asking your question on the new CiviCRM help site.

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: 'How to use custom data in the api' documentation
October 25, 2012, 09:20:20 pm
Surely having kids has taught you to get over the idea small things are cute & to realise small = chaos.
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

Pages: [1] 2
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • 'How to use custom data in the api' documentation

This forum was archived on 2017-11-26.