0

I am creating new extension.
I am adding a new custom field into Activity tab but it's not working

//Creating a custom group for survey

function custom_group_create_survey(){
$survey_custom_group = array(
    "id" => "10",
    "name" => "API_survey",
    "title" => "API survey",
    "extends" => "Activity",
    "extends_entity_column_value" => [
        "1"
    ],
    "style" => "Inline",
    "collapse_display" => "1",
    "weight" => "   3",
    "is_active" => "1",
    "table_name" => "civicrm_value_API_sur__5",
    "is_multiple" => "0",
    "collapse_adv_display" => "0",
    "created_id" => "2",
    "created_date" => "",
    "is_reserved" => "0",
    "is_public" => "1"
);

try{
    $result = civicrm_api3('CustomGroup', 'create', $survey_custom_group);
}
catch (CiviCRM_API3_Exception $e) {
    // Handle error here.
    $errorMessage = $e->getMessage();
    $errorCode = $e->getErrorCode();
    $errorData = $e->getExtraParams();
    return array(
        'is_error' => 1,
        'error_message' => $errorMessage,
        'error_code' => $errorCode,
        'error_data' => $errorData,
    );
}

return $result;

}

function custom_field_create_survey() {
$params = array(
    'custom_group_id' => 10,
    'name' => 'test_textfield2',
    'label' => 'Name1',
    'html_type' => 'Text',
    'data_type' => 'String',
    'default_value' => 'abc',
    'weight' => 4,
    'is_required' => 1,
    'is_searchable' => 0,
    'is_active' => 1,
);

try{
    $result = civicrm_api3('CustomField', 'create', $params);
}
catch (CiviCRM_API3_Exception $e) {
    // Handle error here.
    $errorMessage = $e->getMessage();
    $errorCode = $e->getErrorCode();
    $errorData = $e->getExtraParams();
    return array(
        'is_error' => 1,
        'error_message' => $errorMessage,
        'error_code' => $errorCode,
        'error_data' => $errorData,
    );
}

return $result;

} ##

I am calling these functions into following hook

function surveytab_civicrm_install() {
custom_group_create_survey();
custom_field_create_survey();

_surveytab_civix_civicrm_install(); } ##

This is created by using custom field (administer)

  • Are you getting any error in your CiviCRM log or general error log? – ErikH - CiviCooP Nov 6 '18 at 7:49
  • No any error. I checked in API explorer. – Dipak Giri Nov 6 '18 at 8:37
  • What is the message of the Exception? Or is there no exception? – ErikH - CiviCooP Nov 6 '18 at 8:53
1

I have an example of how I deal with creating custom groups and fields in an extension here: https://lab.civicrm.org/partners/civicoop/dmgeneriek/tree/master/CRM/Dmgeneriek/ConfigItems

I recommend avoiding using hard coded custom group id's in your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.