---
title: "Field and Relation"
site: api
version: 2027.1
versions: "all"
url: https://docs-new.servicely.ai/api/2027.1/scripting/records/field-and-relation/
type: reference
application: platform
last_updated: 2026-12-03
commit: 6777698daac5
---


Field and Relation are the objects behind each field and relation of a [TableRecord](page:scripting/records/table-record). You use them in server scripts to check whether a value changed, read field metadata and add or remove related records.

## Get an instance

Name the field or relation without brackets, or call `getField` or `getRelation`. A Field or Relation taken from a record is connected to it. One taken from a [Table](page:scripting/records/table) is not, and offers only the metadata methods.

```javascript
let field = current.ShortDescription;      // Field, connected to current
let sameField = current.getField("ShortDescription");
let relation = current.getRelation("Group");
let definition = Table("User").getField("Email");  // Field, not connected
```

### Find a field or relation name

On a record form, right-click a field and select **Copy field name** to copy its name to the clipboard.

<Image src="asset:screenshots/api/scripting-records-field-and-relation/copy-field-name-2027.png" size="40">The context menu of the Requested for field on an incident, with Copy field name highlighted.</Image>

For a relation, open the tab that holds the related list, right-click the list's label and select **Copy field name**.

<Image src="asset:screenshots/api/scripting-records-field-and-relation/copy-relation-name-2027.png" size="60">The context menu of the Subtasks related list on an incident, with Copy field name highlighted.</Image>

## Field methods

The first group needs a connected Field.

| Method | Returns | Description | Since |
| --- | --- | --- | --- |
| `field()` | Any | Returns the value. For a reference field, returns the referenced TableRecord. | 1.11 |
| `field(value)` | TableRecord | Sets the value and returns the record. | 1.11 |
| `value()` | Any | Returns the value. For a reference field, returns the ID. `getValue()` is the same. | 1.11 |
| `displayValue()` | String | Returns the value as the UI shows it. `getDisplayValue()` is the same. | 1.11 |
| `hasValue()` | Boolean | Returns `true` if the field has a value. | 1.11 |
| `isEmpty()` | Boolean | Returns `true` if the field has no value. | 1.11 |
| `originalValue()` | Any | Returns the value the field had when the record was loaded. `getOriginalValue()` is the same. | 1.11 |
| `originalDisplayValue()` | String | Returns the display value the field had when the record was loaded. | 1.11 |
| `lastSavedValue()` | Any | Returns the value at the last save. `getLastSavedValue()` is the same. | 1.11 |
| `hasChanged()` | Boolean | Returns `true` if the value differs from the original value. | 1.11 |
| `hasChangedSinceLastSave()` | Boolean | Returns `true` if the value differs from the value at the last save. | 1.11 |
| `validateAndSet(value)` | Object | Validates the value, sets it if it is valid, and returns the validation result. | 1.11 |
| `evaluateScript(scope)` | Any | Code fields only. Runs the script in the field, with the properties of the scope object as variables, and returns its result. | 1.11 |
| `matchesEncrypted(text)` | Boolean | Encrypted fields only. Returns `true` if the text equals the decrypted value. | 1.11 |

The second group works on any Field.

| Method | Returns | Description | Since |
| --- | --- | --- | --- |
| `name()` | String | Returns the field name. `getName()` is the same. | 1.11 |
| `label()` | String | Returns the field label in the user's language. `getLabel()` is the same. | 1.11 |
| `fieldType()` | String | Returns the field type, for example `string` or `boolean`. `getFieldType()` is the same. | 1.11 |
| `tableClass()` | String | Returns the table that defines the field. `getTableClass()` is the same. | 1.11 |
| `tableClassActual()` | String | Returns the table of the record the field belongs to. | 1.11 |
| `targetTableName()` | String | Returns the table a reference field points to. | 1.11 |
| `isConnected()` | Boolean | Returns `true` if the Field belongs to a record. | 1.11 |
| `isReferenceType()` | Boolean | Returns `true` for a reference field. | 1.11 |
| `isRequired()` | Boolean | Returns `true` if the field is required. | 1.11 |
| `isReadOnly()` | Boolean | Returns `true` if the field is read-only. | 1.11 |
| `isUnique()` | Boolean | Returns `true` if the field has a unique constraint. | 1.11 |
| `allowEmptyValues()` | Boolean | Returns `true` if the field accepts an empty value. | 1.11 |
| `isSystemField()` | Boolean | Returns `true` for a system field. | 1.11 |
| `isManagedField()` | Boolean | Returns `true` for a managed field. | 1.11 |
| `isSystemRelationship()` | Boolean | Returns `true` for a system relationship. | 1.11 |
| `isInherited()` | Boolean | Returns `true` if the field comes from a table this table extends. | 1.11 |
| `isSearchable()` | Boolean | Returns `true` if the field type can be searched. | 1.11 |
| `getDefaultValue()` | Any | Returns the field's default value. | 1.11 |
| `getAvailableValues()` | Array | Returns the field's choice values as key and value pairs. | 1.11 |
| `getProperty(name)` | String | Returns the value of a field property. | 1.11 |
| `getI18nKeyName()` | String | Returns the key of the field label. | 1.11 |
| `getI18nKeyDescription()` | String | Returns the key of the field description. | 1.11 |
| `isValidValue(value)` | Boolean | Returns `true` if the value passes the field's validation. | 1.11 |
| `canRead()` | Boolean | Returns `true` if the current user may read the field. | 1.11 |
| `canWrite()` | Boolean | Returns `true` if the current user may change the field. | 1.11 |
| `type()` | String | Returns `field`. | 1.11 |

## Relation methods

| Method | Returns | Description | Since |
| --- | --- | --- | --- |
| `relation()` | TableRecord or Array | Returns the related record, or an array of records for a relation that holds many. | 1.11 |
| `relation(records)` | TableRecord | Replaces the related records with a record, an array of records or the results of a Table query. `null` removes them all. Returns the record. | 1.11 |
| `addRelatedRecord(record)` | TableRecord | Adds a record to the relation, keeping the existing ones. Returns the parent record. | 1.11 |
| `removeRelatedRecord(record)` | TableRecord | Removes a record from the relation. Returns the parent record. | 1.11 |
| `getRecord()` | TableRecord | Returns the related record of a connected single-record relation, or `undefined`. | 1.11 |
| `each(callback)` | Nothing | Calls the function with each related record and its index. `forEach` is the same. | 1.11 |
| `map(callback)` | Array | Returns the results of the function for each related record. | 1.11 |
| `filter(callback)` | Array | Returns the related records for which the function returns `true`. | 1.11 |
| `hasChanged()` | Boolean | Returns `true` if the related records have changed. | 1.11 |
| `isConnected()` | Boolean | Returns `true` if the Relation belongs to a record. | 1.11 |
| `isSingleResult()` | Boolean | Returns `true` if the relation holds at most one record. | 1.11 |
| `relationType()` | String | Returns the relation type: reference, unique reference, many-to-one, one-to-one, many-to-many or generic reference. `getRelationType()` is the same. | 1.11 |
| `name()` | String | Returns the relation name. `getName()` is the same. | 1.11 |
| `label()` | String | Returns the relation label. `getLabel()` is the same. | 1.11 |
| `tableClass()` | String | Returns the table that defines the relation. | 1.11 |
| `parentTableName()` | String | Returns the table on the parent side. | 1.11 |
| `targetTableName()` | String | Returns the table on the target side. | 1.11 |
| `hasIntermediateTable()` | Boolean | Returns `true` if the relation uses an intermediate table, as many-to-many relations do. | 1.11 |
| `intermediateTableName()` | String | Returns the intermediate table name, or `null`. | 1.11 |
| `intermediateTableParentField()` | String | Returns the intermediate table's parent field, or `null`. | 1.11 |
| `intermediateTableTargetField()` | String | Returns the target table name, the same as `targetTableName()`. | 1.11 |
| `getI18nKeyName()` | String | Returns the key of the relation label. | 1.11 |
| `getI18nKeyDescription()` | String | Returns the key of the relation description. | 1.11 |
| `canRead()` | Boolean | Returns `true` if the current user may read the relation. | 1.11 |
| `canWrite()` | Boolean | Returns `true` if the current user may change the relation. | 1.11 |
| `type()` | String | Returns `relation`. | 1.11 |

In the table, `relation` stands for the relation's name, for example `Group`.

<Callout type="info">
  **Changed in 2027.1.** A one-to-one relation, the reverse side of a unique reference, also accepts a record ID: `ci.Asset("<record id>")`. An empty string removes the related record.
</Callout>

## Examples

### Detect a changed value in a trigger

**Servicely Script**

```javascript
if (current.FirstName.hasChanged()) {
    current.Comments("Previous first name: " + current.FirstName.originalValue());
}
```

**Response**

```javascript
// Comments holds the previous value
"Previous first name: <first name>"
```

### Reference field: ID, Field or record

```javascript
let user = Table("User", "<record id>");

user.Organization.value();     // the ID of the organisation
user.Organization;             // the Field object
user.Organization();           // the organisation's TableRecord
user.Organization().Name();    // a field of the organisation
```

Each step to another table is a database query.

<Callout type="warning">
  `user.Organization().Name()` is not safe. If the user has no organisation, `user.Organization()` returns nothing and the call to `Name()` fails with a null reference error. The script stops.
</Callout>

When a step may be empty, use one of these forms instead. Neither fails at an empty step.

```javascript
user.Organization()?.Name();             // optional chaining: undefined if there is no organisation
user.evaluate("Organization.Name()");    // null if there is no organisation
```

[TableRecord](page:scripting/records/table-record) `evaluate()` also follows longer paths in one call. [Querying tables](page:scripting/querying-tables) shows its other forms.

### Set and add related records

```javascript
let user = Table("User", "<record id>");

// Replace the user's groups with the results of a query
user.Group(Table("Group").STARTS_WITH("Name", "Support"));

// Add one group and keep the others
user.Group.addRelatedRecord(Table("Group", "<record id>"));
```

### Read a related list

**Servicely Script**

```javascript
Table("ITSMRequest", "<number>")
    .ParentRequestForITSMRequestTasks
    .map(task => task.displayValue());
```

**Response**

```javascript
["<number>", "<number>"]
```

### Run a script stored in a code field

In this example, `Script` stands for a field of type Code on the record.

```javascript
let rec = Table("<table>", "<record id>");

rec.Script.evaluateScript({
    current: Table("Incident").newRecord(),
    type: "test"
});
```

## Related

- [TableRecord](page:scripting/records/table-record)
- [Table](page:scripting/records/table)
- [Querying tables](page:scripting/querying-tables)
- [Questions](page:scripting/records/questions)
