Create function fields and scripts in the Now Platform to perform common database transformations and calculations.

Regular fields store a value in the database. Instead of storing data, a function field displays the results of a database query. Function fields do not have an associated database column. Instead, function fields generate a value based on simple computations of other fields and constants. They can be used like any other fields in the system: in forms, lists, query conditions, reports, and so on.

Warning: A function field that references another function field in the function will result in an error. Function fields don't hold data or evaluate in run-time.

Example: Use case

You want to identify all incidents with a probably useless short description of fewer than 10 characters.

Instead of querying all records to determine which record meets a given criteria, create a function field or function script that retrieves only the records that meet the criteria.

For a simple computation, it is better to use a function field or function script rather than store a computed value. The value is always calculated on retrieval. Another benefit of using function fields is that the database server performs the transformation rather than the application node.

Function fields versus calculated fields

The values of the calculated fields are stored in the database. The values of function fields or function scripts are not stored in the database, but are calculated at the time of retrieval. Function field values are always up to date.

Function fields versus business rules

Use function fields or scripts when business logic is based on a simple transformation of one or more existing fields. Function fields and scripts let you implement business logic without storing and maintaining the result of a transformation.

Function fields versus filters, query strings, and Rhino

Rather than calling Rhino to perform string transformations, you can use a function field or function script to perform the transformations for you. Function fields and function scripts are more efficient and result in up-to-date values that do not have to be stored or maintained.

Function field limitations

Because function field calculations are not stored in the database, some operations are not relevant.
  • Function fields cannot be directly audited or indexed.
    Note: To index a function field as if it were a regular field, make sure the individual fields used by the function are indexed. Or for best performance, make sure that there is a composite index including all fields that are used in the function.
  • Function fields cannot be encrypted, since a function field value is never stored in the database.
  • Function fields cannot be converted to regular fields or vice versa.
  • Function fields that you create in the Reporting UI do not support dot-walking. For more information, see Configure function fields in Reporting.
  • Security is evaluated on the components of the function and on the calculated value of the field. When used in visualizations, sections that contain information that the user is not permitted to see are hidden from the user.
  • Field function names must be unique.

Defining platform functions

Platform functions can be defined in two ways:
  1. Use the following application programming interfaces (APIs) to build and use functions in a script.
    Table 1. Function APIs
    APIs Description
    Scoped GlideDBFunctionBuilder Construct the function to perform a SQL operation.
    GlideRecord - addFunction(Object function) Apply the function to a GlideRecord.
    GlideDBFunctionCaseBuilder - Global Build case statements.
  2. Create a field that holds the function definition, as shown in the following example.

    Function field definition

    Select the Function field check box on the Dictionary Entry [sys_dictionary] form. This action specifies that the field runs a function rather than stores a value.

glidefunction operations

Function fields can perform the following operations using the glidefunction:<operation> syntax. When providing a field as an argument, you can dot-walk to related fields. For example, cmdb_ci.name.
Note: The function field type must be compatible with the return type of the glide function. For example, integer, longint, string, and so on. If the returned type cannot be converted to the dictionary type, you get exceptions.

Create a function field to perform database functions

Create a field that displays the results of a database function, such as a mathematical operation, field length computation, or day of the week calculation. Test in a subproduction instance before deploying to a production instance.

Before you begin

Role required: personalize_dictionary or admin.

Procedure

  1. Navigate to All > System Definition > Dictionary.
  2. Click New.
  3. Select Function field.
  4. Fill in these fields on the form.
  5. Click Submit.
    You return to the Dictionary Entry [sys_dictionary] table.
  6. Navigate to the table to which you added the function field.
  7. Add the function field to the list.

Result

Use the function field like any other field. For example, add it to a form, use it in a filter, or use it in the condition builder.

If the function definition is invalid, instead of the expected value you receive the message Invalid function if the function field is a string type field, or an empty value for other field types.