Skip to content

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
var customerName = $Xml.Evaluate('Customer/Name');
var total = $Xml.EvaluateNumber('Total');
var dueDate = $Xml.EvaluateDateTime('DueDate');
var isUrgent = $Xml.EvaluateBoolean('IsUrgent');

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
var lineCount = $Xml.EvaluateNumber('count(Lines/Line)');
var fullName = $Xml.Evaluate('concat(Customer/FirstName, " ", Customer/LastName)');

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
// postwork script of an approval task
$Xml.SetValue('Status', 'Approved');
$Xml.SetValue('ApprovedAt', DateTimeOffset.now);
$Xml.SetValue('ApprovedBy', $WorkItem.CompletedBy);
$Xml.SetValue('RejectionReason', null);

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
$Xml.SetDefaultValue('Currency', 'EUR');

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
var total = 0;

$Xml.SelectAll('Lines/Line', function (line) {
    var amount = line.EvaluateNumber('Quantity') * line.EvaluateNumber('UnitPrice');

    line.SetValue('Amount', amount);
    total += amount;
});

$Xml.SetValue('Total', total);

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
// postwork script: keep a history row for every approval
$Xml.AppendChild('Approvals/Approval', function (approval) {
    approval.SetValue('ApprovedBy', $WorkItem.CompletedBy);
    approval.SetValue('ApprovedAt', DateTimeOffset.now);
    approval.SetValue('Comment', $Xml.Evaluate('Comment'));
});

The path must exist in the data model; otherwise an error is thrown.

Removing Rows

Remove removes every node the path selects:

1
$Xml.Remove('Lines/Line[Quantity = 0]');

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
$Xml.Assert('Total > 0', 'The order total must be greater than zero.');
$Xml.Assert('count(Lines/Line) <= 100', 'An order can have at most 100 lines.');

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
var response = $Rest.Create('https://orders.example.com/')
    .Request('orders/{id}')
    .AddUrlParameter('id', $Xml.Evaluate('OrderId'))
    .Get()
    .ToXml();

response.AddNamespace('o', 'http://orders.example.com/schema');

$Xml.SetValue('OrderStatus', response.Evaluate('o:Order/o:Status'));

To parse XML text that is not related to the process data, use Xml.parse(text). See Script Contexts.