Working with Process Data¶
Process data is an XML document shaped by the pool's data model. Scripts read and change it through $Xml, an Xml node that points to the data of the current script. This page explains the patterns server scripts use most: reading values, changing them, working with repeating rows, and checking data before the process continues.
Where $Xml points depends on the script. In task scripts, relative paths start at the work item's data root; in validation rules, $Xml is the bound widget node. Check Script Contexts before reusing a path in another script.
Reading Values¶
Use Evaluate for text and the typed variants when the value is used in a calculation or a comparison:
1 2 3 4 | |
The methods behave differently when the node is missing or empty:
| Method | Missing node in server scripts | Typical use |
|---|---|---|
Evaluate | Empty string | Text, IDs, codes |
EvaluateNumber | 0 | Amounts and quantities |
EvaluateDateTime | null | Dates for $Calendar and comparisons |
EvaluateBoolean | false | Flags |
Because EvaluateNumber returns 0 for a missing node, use IsEmpty or HasNode when "no value" and "zero" mean different things.
Warning
In server scripts, Evaluate throws an error when the path selects more than one node. Select one row with a predicate such as Lines/Line[1]/Product, or read every row with SelectAll.
The path can also be an XPath expression that calculates a value:
1 2 | |
Numbers are read and written in the invariant format, with . as the decimal separator, regardless of the user's culture.
Changing Values¶
SetValue updates every node the path selects. It converts strings, numbers, booleans, DateTimeOffset values and identities to their XML representation, and null clears the value:
1 2 3 4 5 | |
SetValue does not create nodes. When the path selects nothing, the call has no effect, so a typing error in the path does not raise an error. Nodes defined by the data model normally exist; create rows of repeating data with AppendChild.
Use SetDefaultValue to fill a value only when it is still empty, for example in a prework script that must not overwrite what a user already entered:
1 | |
Working with Repeating Rows¶
Reading Rows¶
SelectAll calls a function for every selected node. Inside the function, paths are relative to the row:
1 2 3 4 5 6 7 8 9 10 | |
Without a function, SelectAll returns the nodes as an array. Use SelectSingle to work with one node; it returns null when nothing matches.
Adding Rows¶
AppendChild creates a new node with its child nodes from the data model, calls the function to fill it, and then adds it to the data:
1 2 3 4 5 6 | |
The path must exist in the data model; otherwise an error is thrown.
Removing Rows¶
Remove removes every node the path selects:
1 | |
Synchronizing Rows with Another Source¶
To update rows from a list, for example from a service response, use Sync. It adds missing rows, updates matching rows and removes rows that are no longer in the list. The hash function identifies a row, so it must return a unique value for each row.
Checking Data¶
Use Assert for conditions the data must meet before the process continues. When the XPath condition is false, the script stops with the given message:
1 2 | |
For checks that users should see while they fill a form, use form validation rules instead. Server checks remain necessary because form rules run in the browser.
Reading XML from Other Sources¶
Service responses and stored XML often use namespaces. Register a prefix with AddNamespace before using it in a path:
1 2 3 4 5 6 7 8 9 | |
To parse XML text that is not related to the process data, use Xml.parse(text). See Script Contexts.