Skip to content

Debugging Scripts

When a script does not behave as expected, find out what it did before changing it. This page shows how to write diagnostic output, where to find it, and how to test scripts against external systems safely.

Writing Diagnostic Output

Server scripts write diagnostic entries with console. Each method writes an entry with a level:

Method Level
console.log, console.debug Debug
console.info Information
console.warn Warning
console.error Error

Objects are written as JSON. The message can contain {name} placeholders, which are filled with the following arguments in order:

1
2
3
4
console.info('Order {number} has {count} lines', $Xml.Evaluate('OrderNumber'), $Xml.EvaluateNumber('count(Lines/Line)'));

var response = $Rest.Create('https://api.example.com/').Request('status').Get().ToJson();
console.log(response);

Use console.time and console.timeEnd to measure a slow part of a script.

Write only what helps to find a problem. Do not write passwords, tokens or personal data; diagnostic output can be read by administrators.

Finding the Output

Work items are completed by a background job of the Work Item Processing type. The console output of its scripts is written to the job's log. Open the job in Background Jobs and select the Log tab; see Job Details.

Form scripts run in the browser, so their console output appears in the browser's developer tools.

Reading Diagnostic Traces

A diagnostic trace records what the runtime did for a work item: assignment, prework and postwork scripts, route selection, decisions and external calls such as REST, SOAP and database operations, with their inputs, outputs and timing. Use it to answer questions such as "Did the postwork script run?" or "Which route was selected?".

By default, traces are captured only for instances in test mode. In test mode, the trace of a work item also includes the trace of the previous step, which usually explains why the current step was reached. See Work Item Diagnostics for reading traces, and Diagnostic Trace Configuration for enabling them for live instances.

Stopping Early on Wrong Data

An error far from its cause is hard to find. Check the assumptions of a script where it starts, so that it fails with a message that names the problem:

1
2
Script.Assert($Xml.Evaluate('Customer/Id'), 'A customer must be selected before the order is approved.');
$Xml.Assert('count(Lines/Line) > 0', 'The order has no lines.');

Script.Assert checks a JavaScript value; $Xml.Assert checks an XPath condition on the process data. See Error Handling and Transactions for what happens when the script stops.

Testing Against External Systems

Start the process in test mode while developing it. For instances in test mode, a web service with a Test Mode URL is called at that address instead of the live one; see Web Services.

For REST calls and other integrations, choose the address in the script with $TestMode:

1
2
3
4
5
6
7
var baseUrl = $TestMode ? 'https://test.erp.example.com/api/' : 'https://erp.example.com/api/';

var order = $Rest.Create(baseUrl, 'ERP')
    .Request('orders/{id}')
    .AddUrlParameter('id', $Xml.Evaluate('ErpOrderId'))
    .Get()
    .ToJson();

To see the message exchanged with a SOAP service, ask the call to return the request or the response instead of the result; see SOAP Service Diagnostics.