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 participant fields with api
Pages: [1]

Author Topic: How to use custom participant fields with api  (Read 827 times)

apotropaic

  • I post occasionally
  • **
  • Posts: 34
  • Karma: 1
  • CiviCRM version: 4.4.4
  • CMS version: Wordpress 1.8
  • MySQL version: 5.5.32
  • PHP version: 5.3.10
How to use custom participant fields with api
March 28, 2011, 03:12:36 pm
Hi everybody,

I'm working on creating a participant using the API. I have a custom field that is a Multi-Select for participants. I'm trying to figure out how I can actually set multiple values to that custom field. Here is my code:

Code: [Select]
$params = array(
'contact_id' => 1,
'event_id' => 2,
'status_id' => 6,
'role_id' => 2,
'register_date' => date('YmdHis'),
'source' => 'Online Event Registration: API Testing',
'custom_29' => 'test'
);
                   
$participant =& civicrm_participant_create($params);

print_r($participant);

The participant gets created, but the custom is empty. Whats the trick?

Another unrelated question, why do all the examples always have '=&' when calling an API function? I'm not really sure what this does, but I do it. Thanks,

Andrew

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: How to use custom participant fields with api
March 28, 2011, 07:39:19 pm
Hi

I fixed this in apiv3 & set up a test to check it works - the example is here:

http://svn.civicrm.org/civicrm/trunk/api/v3/examples/ParticipantCreate.php

I think we've already talked about how to backport api v3.

The next few notes are more directed to other members of the A-Team about the api so don't worry too much about them:

1) Irregular behaviour re custom fields is a known problem in api - I copied the code from Event api to participant but it rather makes a mockery of our 'lazy copy' since the way variables get passed around here seems a bit silly. (ie. we replicate $params into custom_format_params but pass in values). Then we merge them afterwards. 3 lines that shoule be one and it looks like we'd better get over trying not to modify $params. Would it even be acceptable to do this in verify mandatory since at least all API call that? or in civicrm_api?

Code: [Select]
        $value = array();
        _civicrm_api3_custom_format_params( $params, $values, 'Participant' );
        $params = array_merge($values,$params); 

2) I wrote a test for it. The test kind of identifies that the only outside variable required is $this->_params which I put in the set up. So, if every set-up defined a success params array then we could just drop this test into all of them. Of course that's not nearly as clever as the approach Xavier is about to suggest...


Code: [Select]
    /**
     * check with complete array + custom field
     * Note that the test is written on purpose without any
     * variables specific to participant so it can be replicated into other entities
     * and / or moved to the automated test suite
     */
    function testCreateWithCustom()
    {
        $ids = $this->entityCustomGroupWithSingleFieldCreate( __FUNCTION__,__FILE__);
       
        $params = $this->_params;
        $params['custom_'.$ids['custom_field_id']]  =  "custom string";
 
        $result = civicrm_api($this->_entity,'create', $params);
        $this->documentMe($params,$result  ,__FUNCTION__,__FILE__);
        $this->assertNotEquals( $result['is_error'],1 ,'in line ' . __LINE__);

        $check = civicrm_api($this->_entity,'get',array('version' =>3, 'id' => $result['id']));
        $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' .$ids['custom_field_id'] ]);
       
        $this->customFieldDelete($ids['custom_field_id']);
        $this->customGroupDelete($ids['custom_group_id']);     

    }
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]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • How to use custom participant fields with api

This forum was archived on 2017-11-26.