TableRecord
Methods of the TableRecord object, which reads, sets, saves, clones and deletes one record in server scripts.
A TableRecord is one record of a table. You use it in server scripts to read and set field values, save the record, and test what changed.
Get an instance
You get a TableRecord from a Table: newRecord() returns a new one, and Table(tableName, id), next() and the iteration methods return saved ones. In a trigger, the current global holds the record being saved.
let rec = Table("User", "<record id>");
let newRec = Table("User").newRecord();
Each field and relation is a method named after it, starting with a capital letter. Call it with no argument to read the value and with one argument to set it. The setter returns the record, so you can chain calls. The Field and Relation page lists what a field or relation offers beyond that.
Field methods
| Method | Returns | Description | Since |
|---|---|---|---|
<FieldName>() |
Any | Returns the field’s value. For a reference field, returns the referenced TableRecord, or null. |
1.11 |
<FieldName>(value) |
TableRecord | Sets the field’s value. A reference field takes a TableRecord or an ID. null empties the field. |
1.11 |
<FieldName> |
Field | The Field object, with no brackets. | 1.11 |
<RelationName>() |
TableRecord or Array | Returns the related record, or an array for a relation that holds many records. | 1.11 |
getFieldValue(fieldName) |
Any | Returns the field’s value. The type depends on the field type. | 1.11 |
setFieldValue(fieldName, value) |
TableRecord | Sets the field’s value. | 1.11 |
getField(fieldName) |
Field | Returns the Field object. | 1.11 |
getRelation(relationName) |
Relation | Returns the Relation object. relation(relationName) is the same. |
1.11 |
hasField(fieldName) |
Boolean | Returns true if the record’s table has the field. |
1.11 |
hasRelation(relationName) |
Boolean | Returns true if the record’s table has the relation. |
1.11 |
getFieldNames() |
Array | Returns the names of the record’s fields. | 1.11 |
getRelationNames() |
Array | Returns the names of the record’s relations. | 1.11 |
apply(values) |
TableRecord | Sets every field named in the object. Throws an error listing the problems if a value is not valid. | 1.11 |
applyExplicit(values) |
TableRecord | Same as apply, but also empties the fields the object leaves out. |
1.11 |
validateAndSetFieldValues(values) |
Object | Validates and sets the values in the object and returns the validation result instead of throwing an error. | 1.11 |
evaluate(path) |
Any | Follows a dotted path of reference fields and returns the value, field or record at its end, or null if a step is empty. |
1.11 |
evaluate(path, onSuccess, onFailure) |
Any | Follows the path and calls onSuccess with what it finds, or onFailure if a step is empty. Returns what the function returns. |
1.11 |
getFieldDefinitionByPath(path) |
Field | Returns the field at the end of a dotted path, not connected to a record. | 1.11 |
apply and applyExplicit set system and managed fields only when the record’s checks allow it.
Save methods
| Method | Returns | Description | Since |
|---|---|---|---|
create() |
TableRecord | Saves the record. A new record is inserted, a saved one is updated. | 1.11 |
update() |
TableRecord | Same as create(). |
1.11 |
update(auditReason) |
TableRecord | Updates the record and stores the reason with the audit entry. | 1.11 |
upsert(fieldName) |
TableRecord | Updates the record that has the same value in this field, or creates one if none does. | 1.11 |
upsert(fieldNames) |
TableRecord | Same as upsert(fieldName), matching on every field in the array. |
1.11 |
delete() |
TableRecord | Deletes the record. | 1.11 |
clone(options) |
TableRecord | Returns an unsaved copy of the record. Call create() to save it. The option cloneRelationships, true by default, also copies many-to-many relations. |
1.11 |
clone(cloneRelationships) |
TableRecord | Deprecated. Copies the record and saves the copy at once. Use clone(options). |
1.11 |
withCRUDOption(key, value) |
TableRecord | Sets an option for the next save or delete, for example to name the workflow to use. | 1.11 |
A clone does not copy system fields, WorkflowStatus, SLAStatus or fields with a unique constraint. The exception is a required text field with a unique constraint, which gets a changed copy of the value. Records of a few security tables cannot be created, changed or deleted from any script.
State methods
| Method | Returns | Description | Since |
|---|---|---|---|
getID() |
String | Returns the record’s ID. ID() is the same. |
1.11 |
getTableName() |
String | Returns the name of the record’s table. | 1.11 |
getTableDisplayName() |
String | Returns the table’s label in the user’s language. | 1.11 |
getTableI18n() |
String | Returns the key of the table’s label. | 1.11 |
getTableDefinition() |
Object | Returns the table definition record. | 1.11 |
isChildOf(tableName) |
Boolean | Returns true if the record’s table extends the named table. |
1.11 |
displayValue() |
String | Returns the record’s display value. getDisplayValue() is the same. |
1.11 |
isNewRecord() |
Boolean | Returns true if the record has not been saved. |
1.11 |
isActive() |
Boolean | Returns the value of the record’s active field, or true if the table has none. |
1.11 |
isDeleted() |
Boolean | Returns true if other logic has deleted the record. |
1.11 |
hasChanged() |
Boolean | Returns true if a field has changed since the record was loaded. Stays true after a save. |
1.11 |
hasChangedSinceLastSave() |
Boolean | Returns true if a field has changed since the record was last saved. |
1.11 |
doesRecordViolateUniqueConstraints() |
Boolean | Returns true if saving the record would break a unique constraint. |
1.11 |
canRead() |
Boolean | Returns true if the current user may read the record. |
1.11 |
canWrite() |
Boolean | Returns true if the current user may change the record. |
1.11 |
canDelete() |
Boolean | Returns true if the current user may delete the record. |
1.11 |
matchesCriterion(criterion) |
Boolean | Returns true if the record matches the criterion. |
1.11 |
matchesCriterion(jsonQuery) |
Boolean | Same, with the criterion as a JSON string. | 1.11 |
getLinkForUI() |
String | Returns the link to the record in the Servicely UI. | 1.11 |
getLinkForUI(options) |
String | Returns a link built from the options aspect, objectOnly and relative, as for Table. |
1.11 |
toFieldValueMap() |
Object | Returns the field values keyed by field name. | 1.11 |
toFieldDisplayValueMap() |
Object | Returns the display values keyed by field name. | 1.11 |
asJson(fieldNames) |
Object | Returns the fields that have a value, leaving out password and encrypted fields. With an array of names, returns only those fields. | 1.11 |
hasQuestions() |
Boolean | Returns true if the record was created from a catalog item with questions. |
1.11 |
questions() |
Questions | Returns the catalog questions and answers, or null. See Questions. |
1.11 |
costs() |
Object | Returns the catalog cost line items and totals, or null. |
1.11 |
Check methods
These methods change the checks that apply when the record is saved. The checks are described in TableProtected and TableChecks.
| Method | Returns | Description | Since |
|---|---|---|---|
tableChecks() |
TableChecks | Returns the record’s checks. getTableChecks() is the same. |
1.11 |
tableChecks(checks) |
TableRecord | Replaces the record’s checks. | 1.11 |
withTableChecks(checks, callback) |
Any | Runs the function with these checks, then restores the previous ones. Returns what the function returns. | 1.11 |
enableTriggers(flag) |
Boolean or TableRecord | With no argument, returns whether triggers run. With true or false, sets it. |
1.11 |
enableValidation(flag) |
Boolean or TableRecord | Reads or sets whether validation runs. | 1.11 |
enablePermissionChecks(flag) |
Boolean or TableRecord | Reads or sets whether permissions are checked. | 1.11 |
enableSystemFieldUpdate(flag) |
Boolean or TableRecord | Reads or sets whether the save updates system fields such as UpdatedOn. |
1.11 |
enableSystemFieldRestrictions(flag) |
Boolean or TableRecord | Reads or sets whether system fields are protected from change. | 1.11 |
enableManagedFieldRestrictions(flag) |
Boolean or TableRecord | Reads or sets whether managed fields are protected from change. | 1.11 |
enableAudit(flag) |
Boolean or TableRecord | Reads or sets whether the save is audited. | 1.11 |
disableTriggers() |
TableRecord | Deprecated. Stops triggers running. Use withTableChecks. |
1.11 |
disableValidation() |
TableRecord | Deprecated. Stops validation. | 1.11 |
disablePermissionChecks() |
TableRecord | Deprecated. Stops permission checks. | 1.11 |
disableSystemFieldUpdate() |
TableRecord | Deprecated. Stops the update of system fields. | 1.11 |
disableSystemFieldRestrictions() |
TableRecord | Deprecated. Allows changes to system fields. | 1.11 |
disableManagedFieldRestrictions() |
TableRecord | Deprecated. Allows changes to managed fields. | 1.11 |
disableAllValidation() |
TableRecord | Deprecated. Calls the five methods above. Triggers still run. | 1.11 |
The deprecated methods write a warning to the log each time they run.
Examples
Read a field value
The three forms return the same value.
Servicely Script
let user = Table("User", EQUAL("UserName", "<user name>"));
user.FirstName();
user.getFieldValue("FirstName");
user.getField("FirstName").value();
Response
"<first name>"
Set field values and save
Setters return the record, so a chain can end with update().
Table("User", EQUAL("UserName", "<user name>"))
.FirstName("<first name>")
.BirthDate(DateTime().withDate(1970, 6, 11))
.update();
A reference field takes the record or its ID. null empties any field.
let schedule = Table("Schedule", "<record id>");
let user = Table("User", "<record id>");
user.Schedule(schedule); // a record
user.Schedule(schedule.getID()); // or its ID
user.Schedule(null); // empty the field
user.update();
Clone a record
Servicely Script
let copy = Table("User", "<record id>")
.clone({ cloneRelationships: true })
.UserName("<user name>")
.create();
copy.isNewRecord();
Response
false
Change checks for one block
withTableChecks scopes the change, so the checks on current are back to normal after the block. This is the safe way to set a protected field from a trigger.
current.withTableChecks(TableChecks.NoTableChecksNoSystemFieldsNoChecksNoEvents, () => {
current.ProtectedField("<value>");
});