- External custom fields
  - Description
  - Introduction into writing source of values
  - Configuration
  - How to write custom source

# External custom fields

## Description

`External custom fields` is an extension to custom fields that allow you to define CFs with dynamic lists of values. Loading values into these custom fields requires writing a little Perl code to fetch the data from the external source.

## Introduction into writing source of values

For each type of data source that you want, you'll need to put a file in /opt/rt4/local/lib/RT/CustomFieldValues/ (or equivalent if you installed RT into someplace other than /opt/rt4). To get a sense of the code that you'll need to write, take a look at the code in /opt/rt4/lib/RT/CustomFieldValues/Groups.pm for a simple example which just uses RT's API to pull in a list of RT's groups.

Running `perldoc /opt/rt4/lib/RT/CustomFieldValues/External.pm` will show you the documentation for the API that needs to be fulfilled; copying and editing the `Groups` example is probably a fine place to start.

Later in this doc we'll describe the example a little bit more.

## Configuration

After the custom code is written, you need to tell RT about its existence by adding something like following to your RT_SiteConfig.pm:

        Set(@CustomFieldValuesSources, "RT::CustomFieldValues::MySource");

The value in quotes should be the name of the class that you created.

Stop and start your web server to enable any config changes. Open the web interface as an administrative user (such as root), and create new custom field. Set its type to be a Select or Autocomplete field, and save the changes. You should now you have ability to select a "source" for values. Choose the class you wrote from the list and the save changes.

## How to write custom source

You have to implement a subclass of RT::CustomFieldValues::External. There are two main methods you want to override:

SourceDescription

  This method should return a string describing the data source; this is the identifier which the administrator will see in the dropdown in the web interface. See "Configuration".

ExternalValues

  This method should return an array reference of hash references. The hash references should contain keys for `name`, `description`, and `sortorder`. `name` is most important one; the others are optional. You can also optionally provide a key for `category` and use the "Categories are based on" option on the custom field configuration page to make the values displayed for this custom field vary based on the value selected in the "based on" custom field.

Here's a simple static example:

      package RT::CustomFieldValues::MySource;

      # define class inheritance
      use base qw(RT::CustomFieldValues::External);

      # admin friendly description, the default valuse is the name of the class
      sub SourceDescription {
          return 'My Source';
      }

      # actual values provider method
      sub ExternalValues {
          # return reference to array ([])
          return [
              # each element of the array is a reference to hash that describe a value
              # possible keys are name, description, sortorder, and category
              { name => 'value1', description => 'external value', sortorder => 1, category => 'Other CF' },
              { name => 'value2', description => 'another external value', sortorder => 2, category => 'Other CF' },
              # values without description are also valid, the default description is empty string
              { name => 'value3', sortorder => 3 },
              # you can skip sortorder too, but note that the default sortorder is 0 (zero)
              { name => 'value3' },
          ];
      }

      1; # don't forget to return some true value

← Back to index