Table
Methods of the Table object, which queries a table, reads its schema and creates, counts and deletes its records in server scripts.
Table is the server-side object for one table: you build a query on it, run it, read the table’s fields and relations, and create records. It is a global in every server script, such as triggers, script libraries, jobs, transforms and inbound message scripts.
Get an instance
Call Table with a table name. The other arguments are optional and can come in any order.
| Call | Returns | Description | Since |
|---|---|---|---|
Table(tableName) |
Table | A Table you can query. Nothing runs until you call a query or result method. | 1.11 |
Table(tableName, id) |
TableRecord | The record with this ID, or with this value in the table’s lookup field. Returns null if there is none. |
1.11 |
Table(tableName, criterion) |
TableRecord | Runs the criterion and returns the one matching record. Returns null if nothing matches and throws PLATFORM-10003 if more than one record matches. |
1.11 |
Table("#/" + tableName + "/" + id) |
TableRecord | The same lookup, written as the path part of a record link. | 1.11 |
Table(tableName, checks) |
Table | A Table that uses this set of TableChecks instead of the default. | 1.11 |
Table applies validation, triggers, field values and events, but not permissions, record restrictions or tenancy. Use TableProtected with the same arguments to apply them all. Both are described in TableProtected and TableChecks.
let users = Table("User");
let user = Table("User", "<record id>");
let userByName = Table("User", EQUAL("UserName", "<user name>"));
Naming convention
The Table API uses case to keep methods, fields and query operators apart.
| Kind | Convention | Example |
|---|---|---|
| Methods | Start with a lower-case letter | rec.isNewRecord() |
| Fields and relations | Start with an upper-case letter, in CamelCase | rec.ShortDescription() |
| Query operators | All capitals | Table("User").EQUAL("Active", true) |
Query methods
These methods build and run a query. Each returns the Table, so you can chain them. The operators you add with them are in Query operators and criteria.
| Method | Returns | Description | Since |
|---|---|---|---|
query() |
Table | Runs the query built so far. Result methods run it for you if you have not. | 1.11 |
execute() |
Table | Same as query(). |
1.11 |
addCriteria(criterion) |
Table | Adds a criterion built with the query operators. | 1.11 |
addJsonQuery(jsonQuery) |
Table | Adds a query in the JSON form the REST API uses, as a string or an object. The JSON can also set the first result and the maximum number of results. | 1.11 |
orderBy(fieldName) |
Table | Sorts the results by the field, ascending. | 1.11 |
orderByDesc(fieldName) |
Table | Sorts the results by the field, descending. | 1.11 |
maxResults(count) |
Table | Sets the maximum number of records the query returns. | 1.11 |
limit(count) |
Table | Same as maxResults(count). |
1.11 |
firstResult(position) |
Table | Sets the zero-based position of the first record returned. | 1.11 |
offset(position) |
Table | Same as firstResult(position). |
1.11 |
onNoResult(callback) |
Table | Sets a function that runs, with the Table as its argument, when the query returns no records. | 1.11 |
sql() |
String | Returns the SQL the query generates, with placeholders for the values. | 1.11 |
sql(true) |
String | Returns the SQL with the values written in. | 1.11 |
A query without maxResults returns at most 10,000 records. The limit comes from the script.query.default_max_results application property.
Result methods
These methods run the query if it has not run, then return or process its records.
| Method | Returns | Description | Since |
|---|---|---|---|
hasNext() |
Boolean | Returns true if there is another record to read. |
1.11 |
next() |
TableRecord | Returns the next record, or null when there are none left. |
1.11 |
firstOrNull() |
TableRecord | Same as next(). |
1.11 |
get(id) |
TableRecord | Returns the record with this ID or lookup value, or null. |
1.11 |
get(criterion) |
TableRecord | Adds the criterion and returns the first matching record, or null. |
1.11 |
executeSingle() |
TableRecord | Returns the only matching record, or null if none. Throws an error if more than one record matches. |
1.11 |
executeSingle(callback) |
Any | Calls the function with the only matching record and returns its result. With no match, returns the result of the onNoResult function, or null. |
1.11 |
each(callback) |
Nothing | Calls the function with each record and its zero-based index. | 1.11 |
forEach(callback) |
Nothing | Same as each(callback). |
1.11 |
forEachPaginated(callback) |
Nothing | Same as forEach, but reads the records from the database a page at a time. Use it for large result sets. |
1.11 |
map(callback) |
Array | Calls the function with each record and its index and returns the results. A result of undefined is left out, so return null to keep a blank entry. |
1.11 |
flatMap(callback) |
Array | Same as map, but an array returned by the function is merged into the result. |
1.11 |
filter(callback) |
Array | Returns the records for which the function returns true. |
1.11 |
resultCount() |
Integer | Returns the number of records the query returned, within maxResults. |
1.11 |
totalResultCount() |
Integer | Returns the number of records that match the query, ignoring maxResults. |
1.11 |
To count records without reading them, use Aggregate.
Record and schema methods
| Method | Returns | Description | Since |
|---|---|---|---|
newRecord() |
TableRecord | Returns a new, empty record. It is saved only when you call create() on it. |
1.11 |
recordExists(id) |
Boolean | Returns true if a record has this ID or lookup value. |
1.11 |
recordExists(criterion) |
Boolean | Returns true if a record matches the criterion. |
1.11 |
recordExistsInDB(id) |
Boolean | Returns true if the database holds a record with this ID. |
1.11 |
deleteMultiple() |
Integer | Deletes every record the query matches and returns how many it deleted. Each delete runs triggers and audit, and records the user cannot delete are skipped. | 1.11 |
updateMultiple(values) |
Integer | Sets the fields in the object on every matching record in one database statement and returns the number updated. Triggers do not run. | 1.11 |
canRead() |
Boolean | Returns true if the current user may read the table. |
1.11 |
tableChecks() |
TableChecks | Returns the checks the Table applies. getTableChecks() is the same. |
1.11 |
tableChecks(checks) |
Table | Replaces the checks the Table applies. | 1.11 |
getAggregate() |
Aggregate | Returns an Aggregate for the table. | 1.11 |
getField(fieldName) |
Field | Returns the field, not connected to a record. | 1.11 |
getFields() |
Object | Returns the table’s fields and single-record relations, keyed by name. | 1.11 |
getRelation(relationName) |
Relation | Returns the relation, not connected to a record. | 1.11 |
getRelations() |
Object | Returns all the table’s relations, keyed by name. | 1.11 |
hasField(fieldName) |
Boolean | Returns true if the table has the field. |
1.11 |
hasRelation(relationName) |
Boolean | Returns true if the table has the relation. |
1.11 |
getLookupField() |
Field | Returns the field used to look up a record by value. | 1.11 |
getTableName() |
String | Returns the table name. | 1.11 |
displayValue() |
String | Returns the table’s label in the user’s language. getDisplayValue() is the same. |
1.11 |
displayI18n() |
String | Returns the key of the table’s label. | 1.11 |
getTableDefinition() |
Object | Returns the table definition record. | 1.11 |
getParentTableHierarchyInclusive() |
Array | Returns the table and the tables it extends. | 1.11 |
getParentTableHierarchyExclusive() |
Array | Returns the tables this table extends. | 1.11 |
getChildInheritanceTreeInclusive() |
Array | Returns the table and the tables that extend it. | 1.11 |
getChildInheritanceTreeExclusive() |
Array | Returns the tables that extend this table. | 1.11 |
getLinkForUI() |
String | Returns the link to the table’s list in the Servicely UI. | 1.11 |
getLinkForUI(options) |
String | Returns a link built from the options create, aspect, objectOnly and relative. |
1.11 |
The Table also accepts the TableChecks methods, such as withTriggersEnabled(false), to change its checks.
Examples
Create a record
newRecord() prepares the record. create() saves it.
Servicely Script
let newUser = Table("User")
.newRecord()
.UserName("<user name>")
.create();
newUser.isNewRecord();
Response
false
Check whether a record exists
Servicely Script
Table("User")
.recordExists(EQUAL("UserName", "<user name>"));
Response
true
Read the results
next() returns one record at a time. map() returns an array built from every record.
Servicely Script
Table("User")
.EQUAL("Active", true)
.orderBy("Name")
.maxResults(3)
.map(user => user.UserName());
Response
["<user name>", "<user name>", "<user name>"]
List the fields of a table
getFields() returns fields and single-record relations. getRelations() returns every relation, including those that hold many records.
Servicely Script
Table("User")
.getFields()
Response
{
Active=[field: boolean],
Email=[field: string],
FirstName=[field: string],
UserName=[field: string],
Manager=[Relation User.Manager],
Organization=[Relation User.Organization],
...
}
Build a link to the UI
| Option | Effect on the link |
|---|---|
create: true |
Adds /_create, which opens a new record form. |
aspect: "<aspect>" |
Adds the aspect, for example SelfService, to the end of the link. |
objectOnly: true |
Adds ! before #, which shows the object without the surrounding menus. |
relative: true |
Leaves out the instance address. |
Servicely Script
Table("User")
.getLinkForUI({ create: true, aspect: "SelfService" });
Response
"https://<instance>/#/User/_create/SelfService"
Delete matching records
Servicely Script
Table("User")
.EQUAL("Active", false)
.deleteMultiple();
Response
// The number of records deleted
5