Function field
- UpdatedJan 30, 2025
- 8 minutes to read
- Yokohama
- Platform Field Administration
Create function fields and scripts in the ServiceNow AI 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, data visualizations, and so on.
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
- 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
- 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. - Create a field that holds the function definition, as shown in the following
example.

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
glidefunction:<operation> syntax. When providing a field as an
argument, you can dot-walk to related fields. For example,
cmdb_ci.name.| Operation | Description | Example |
|---|---|---|
| add() | Takes two number fields as input, adds them, and returns
the results as a field value. This function also takes numerical values for either input. Place numerical values in single or double quotation marks. |
glidefunction:add(child_incidents, parent_incident)
Returns 6 if the incident has five child incidents and one parent incident. Possible return types: Decimal, Floating Point Number, Large Whole Number, Whole Number |
| coalesce() | Takes any number of comma-separated fields as input and returns the first non-empty value. | glidefunction:coalesce(closed_at, resolved_at, sys_updated_on)
If the value of closed_at is empty, the function returns the value of resolved_at. If the value of resolved_at is also empty, the function returns the value of sys_updated_on. Return type: Text |
| concat() | Takes any number of comma-separated fields and constants as input, concatenates the input, and returns a single string as a field value. | glidefunction:concat(incident_number, '/', short_description)
Returns "INC0001 / My client needs a new laptop." if the value of the number field is 'INC0001' and the short_description is 'My client needs a new laptop'. Return type: Text |
| datediff() | Takes two date/time fields as input, calculates the difference between the dates in days, minutes, and seconds, and returns the results as a duration field value. | glidefunction:datediff(closed_at, sys_created_on)
Returns the duration of an incident from the creation date to the close date. Example result: 10 days, 8 hours 23 minutes 11 seconds Return type: Duration |
| dayofweek() | Takes two arguments: A date field and a constant of either '1' (week starts on Sunday) or '2' (week starts on Monday). Returns the results as an
integer value that represents the day of the week. The dayofweek() function uses UTC dates, but adjusts comparison values based on the instance's time zone. |
glidefunction:dayofweek(resolved_at, '1'). If resolved_at occurs on a Wednesday, returns 4 if the integer is 1 and returns 3 if the integer is 2. Return type: Whole number |
| distance_sphere() | Takes two locations and returns the distance between them in meters. |
Return type: Decimal |
| divide() | Takes two number fields as input, divides the first by the second, and returns the result as a field value. This function also takes numerical values for either input. Place numerical values in single or double quotation marks. |
glidefunction:divide(u_num2,u_num1)
Returns 5 if num2 = 10 and num1 = 2. Possible return types: Decimal, Floating Point Number, Large Whole Number, Whole Number |
| get_latitude() | Takes any geo point column or numeric field or numeric constant and converts it to a valid latitude value within the [-90,90] range. |
Return type: Floating point number |
| get_longitude() | Takes any geo point column or numeric field or numeric constant and converts it to a valid longitude value within the [-179,180] range. |
Return type: Floating point number |
| greatest() | Takes two or more values as input and returns the greatest value of the list of arguments. | glidefunction:greatest(10,100,1000)
Returns 1000 Possible numeric return types: Decimal, Floating Point Number, Large Whole Number, Whole Number This function also works with date and string fields. |
| least() | Takes two or more values as input and returns the lowest value of the list of arguments. | glidefunction:least(10,100,1000)
Returns 10 Possible numeric return types: Decimal, Floating Point Number, Large Whole Number, Whole Number This function also works with date and string fields. |
| length() | Takes a string field as input, calculates the field length in characters, and returns the results as a field value. | glidefunction:length(short_description)
Returns 37 if short_description = "This application is performing a test". Return type: Whole number |
| multiply() | Takes two number fields as input, performs the
multiplication, and returns the results as a field value. This function also takes numerical values for either input. Place numerical values in single or double quotation marks. |
glidefunction:multiply(u_num1, u_num2)
Returns 48 if num1 = 8 and num_2 = 6. Possible return types: Decimal, Floating Point Number, Large Whole Number, Whole Number |
| position() | Takes two text fields or two text fields and a whole number as input. One or both of the text fields can also be strings.
Returns 0 if the first text field is not present in the second (after the position of the whole number if specified). |
Return type: Whole number |
| substring() | Takes a text field and two whole numbers as input. Returns the first instance of a string that starts at the position of the first whole number and is the length of the second. | glidefunction:substring(short_description, '7', '2')
If the value of the short_description field is 'We're going to the store', returns 'go'. Return type: Text |
| subtract() | Takes two number fields as input, subtracts the second from the first, and returns the result as a field value. This function also takes numerical values for either input. Place numerical values in single or double quotation marks. |
glidefunction:subtract(u_num1, u_num2)
Returns 2 if num1 = 8 and num_2 = 6. Possible return types: Decimal, Floating Point Number, Large Whole Number, Whole Number |
| to_geopoint() | Takes latitude and longitude columns or numerical values and converts them to a geo point column. |
Return type: Geo point |
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
Procedure
Result
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.