Reading and Saving Table Data¶
Server scripts read and write the tables of relational database schemas through $Database. This page shows how to query rows, filter and summarize them with expressions, copy them to and from process data, and save changes efficiently.
Table writes are part of the operation's transaction: when the script fails, they are discarded together with the process data changes. See Error Handling and Transactions.
Reading Rows¶
$Database.Get runs a query and returns a DataTable. Inside Each, this is the row and columns are read as properties:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
Without Columns, all columns of the table are returned. Use Start and MaxLength to read a page of rows. When only one column must match a value, GetData is shorter:
1 | |
Column values are converted to JavaScript values; for example, DateTime fields become DateTimeOffset objects. See Field Values in Server Scripts.
Filtering Rows¶
Criteria are combined with And. Use Comparison for other operators and Condition : 'Or' with Blocks to group conditions. A criterion with IgnoredValues is skipped when its value is one of the listed values, which makes form filters optional:
1 2 3 4 5 6 7 8 9 10 11 12 | |
With Like, * in the value is a wildcard; a value without a wildcard matches anywhere in the text. See QueryCriteria for all options.
Using Expressions¶
Columns, criteria and orders are defined with expressions. An expression can follow relations, such as Customer.Region.Name, calculate values, such as Quantity * UnitPrice, and use date and aggregate functions. Joins and grouping are created from the expressions, so the query does not describe them:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
Aggregate columns group the result by the other columns. To compare two columns, set ValueType : 'Expression', for example { Expression : 'ShipDate', Value : 'DueDate', ValueType : 'Expression', Comparison : 'GreaterThan' }.
Use Expression instead of the older Formula property. { Name : 'Amount', Formula : 'Sum' } still works, but { Name : 'Amount', Expression : 'Sum(Amount)' } is clearer and can also contain calculations. See QueryColumn.Expression and Relational Database Query for the syntax.
Copying Rows to Process Data¶
ExportToXml runs a query and creates a node for each row at the given path. Columns are matched to child nodes with the same name:
1 2 3 4 5 6 7 8 9 10 11 | |
Use a Map function when the names differ. Inside it, this is the row and the argument is the new node.
Saving Process Data to a Table¶
ImportFromXml saves each node selected by the path as a row. Rows are matched by primary key: existing rows are updated and new rows are inserted, so running it again does not create duplicates:
1 2 3 4 5 6 7 8 | |
ExportToXml and ImportFromXml require a unique primary key in the table and in the data model.
Changing Rows¶
For a single row, AddData inserts a row and EnsureData inserts or updates the row with the given key. Both save at once:
1 2 3 4 5 6 7 8 | |
The key is not copied to a new row automatically; include it in the values, as above. The last argument is applied only when a row is inserted.
For many rows, change a table in memory and call Save once. Calling AddData or EnsureData in a loop saves on every call. Use $Database.Empty to insert rows, and ToDictionary to update existing rows by key:
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
To delete rows, call DeleteData with a column and value, or mark rows with Delete and call Save:
1 2 3 4 5 6 7 8 9 | |
External Databases¶
ExecuteQuery and ExecuteNonQuery run SQL on a database connection defined in the domain settings. The SQL is sent to the database as written, so pass values as parameters in the syntax of that database:
1 2 3 4 5 6 7 | |
The returned table is read-only; use ExecuteNonQuery to change data. Changes in an external database are not part of the operation's transaction.