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 (Moderator: Donald Lobo) »
  • Problem adding custom fields with v2.0
Pages: [1]

Author Topic: Problem adding custom fields with v2.0  (Read 1475 times)

dougall

  • Guest
Problem adding custom fields with v2.0
March 25, 2008, 06:53:02 am
Hi,

I'm adding a custom field using the following code:
Code: [Select]
    $fieldParms = array(
                     'custom_group_id'  => $customGroupID,
                     'name'             => 'cerkle_uid',
                     'label'            => 'Cerkle UserID',
                     'data_type'        => 'Integer',
                     'html_type'        => 'Text',
                     'is_required'      => 0,
                     'weight'           => 4,
                     'is_active'        => 1,
                     'is_searchable'    => 1
                    );
    echo "<br>Adding custom field 'cerkle_uid' to the new custom group...<br>";
    echo "Using parms: ".print_r($fieldParms,true)."<br>";
    $customField = civicrm_custom_field_create($fieldParms);
    echo "Returned value: ".print_r($customField,true)."<BR>";

But I'm getting an error saying "Missing Required field :custom_group_id", even though I can see it's there because of the print_r($fieldParms), the full output from the above code is:
Quote
Adding custom field 'cerkle_uid' to the new custom group...
Using parms: Array ( [custom_group_id] => 5 [name] => cerkle_uid [label] => Cerkle UserID [data_type] => Integer [html_type] => Text [is_required] => 0 [weight] => 4 [is_active] => 1 [is_searchable] => 1 )
Returned value: Array ( [is_error] => 1 [error_message] => Missing Required field :custom_group_id )

If I place the group id into an array, in the manner suggested by the API docs: http://wiki.civicrm.org/confluence/display/CRMDOC/Object+Properties+and+Custom+Data+APIs#ObjectPropertiesandCustomDataAPIs-crmCreateCustomField
e.g. $customGroupId = array ('id' => 12);
Then I get a different error:
Quote
Database Error Code: Cannot add or update a child row: a foreign key constraint fails (`civicrm/civicrm_custom_field`, CONSTRAINT `FK_civicrm_custom_field_custom_group_id` FOREIGN KEY (`custom_group_id`) REFERENCES `civicrm_custom_group` (`id`) ON DELETE CASCADE), 1452

Any suggestions?

Thanks, Dougall

« Last Edit: March 25, 2008, 08:26:20 am by dougall »

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: Problem adding custom fields with v2.0
March 25, 2008, 10:15:08 am

Can you please file an issue for this and we'll fix it before the 2.0.2 release (tomorrow)

thanx

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

dougall

  • Guest
Re: Problem adding custom fields with v2.0
March 25, 2008, 11:32:05 am
Whoops my mistake ... now that i've read more thoroughly through api/v2/CustomGroups.php I see that the way in which parameters are passed to the civicrm_custom_field_create is different to the old crm_create_custom_group.

I am now placing my field parameters array inside a second array under the key "fieldParams" and this fixes it.

My revised code is:
Code: [Select]
  $fieldParams = array(
                     'custom_group_id'  => $customGroupID,
                     'name'             => 'cerkle_uid',
                     'label'            => 'Cerkle UserID',
                     'data_type'        => 'Integer',
                     'html_type'        => 'Text',
                     'is_required'      => 0,
                     'weight'           => 4,
                     'is_active'        => 1,
                     'is_searchable'    => 1
                    );
    echo "<br>Adding custom field 'cerkle_uid' to the new custom group...<br>";
    echo "Using params: ".print_r($fieldParams,true)."<br>";

    $params = array ("fieldParams" => $fieldParams);

    $customField = civicrm_custom_field_create($params);
    echo "Returned value: ".print_r($customField,true)."<BR>";


One thing I did notice however was that if the field params array is missing the field "label" then the error message says "Missing Required field :custom_group_id" rather than "Missing Required field :label" ... looks like i get to file an issue after all ... Dougall

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Problem adding custom fields with v2.0

This forum was archived on 2017-11-26.