---
title: "Field and Relation"
site: api
version: 1.11
versions: "all"
url: https://docs-new.servicely.ai/api/1.11/scripting/field-and-relation/
type: reference
application: platform
last_updated: 2026-12-02
commit: f6d7027b3395
---


Field and Relation are the objects behind each field and relation of a [TableRecord](page:scripting/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/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-field-and-relation/copy-field-name.png" size="20">The field's context menu with Copy field name highlighted.</Image>

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

<Image src="asset:screenshots/api/scripting-field-and-relation/copy-relation-name.png" size="30">The context menu of a related list 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`.


## Examples

### Detect a changed value in a trigger

<CodeTabs>
  Servicely Script

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

<CodeTabs>
  Response

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

### 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. Use [TableRecord](page:scripting/table-record) `evaluate()` when a step may be empty.

### 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

<CodeTabs>
  Servicely Script

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

<CodeTabs>
  Response

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

### 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/table-record)
- [Table](page:scripting/table)
- [Querying tables](page:scripting/querying-tables)
- [Questions](page:scripting/questions)
