Templating Step Settings with Liquid¶
Most workflow step settings can take their value from the work item at run time instead of a fixed value. A preprocessor computes the setting right before the step runs. There are three:
- Liquid Template
- Javascript
- Global Variable
You choose one with the gear icon next to each setting's name in the workflow designer. Use a preprocessor when a setting depends on the work item, for example a request body that carries the invoice number, or a URL that comes from a global variable.
At a glance
- Runs On the workflow worker, right before the step starts, each time the step runs.
- Language Liquid (DotLiquid), a JavaScript expression (V8), or a global variable's value.
- Authored in The step dialog in the Workflow designer: the gear icon next to the setting's name.
- Can reach The work item (read only). The Javascript preprocessor also reads global variables. No network.
- Best for Request bodies and URLs for Call External API, email text, names and paths built from metadata.
How a setting gets its value¶
graph TD
A["Work item reaches the step"] --> B["Gear choice for each setting"]
B -->|Value| F["Fixed text"]
B -->|Liquid Template| L["Render with WorkItem"]
B -->|Javascript| J["Evaluate with workItem and GlobalVariables"]
B -->|Global Variable| G["Variable value for this work item"]
L --> T["Trim, then read as the setting's type"]
J --> T
G --> T
F --> S["Step runs with the final values"]
T --> S
If a preprocessor throws, or the result cannot be read as the setting's type, the step fails before it runs, unless Fallback to Value applies (see below).
Where you set it¶
- Open the step in the Workflow designer (double-click it).
- Select the gear icon next to the setting's name.
- Choose Liquid Template, Javascript, Global Variable, or Value (a fixed value, the default).
- Write the template or script, or pick the variable. Two options apply to all three preprocessors:
- Trim White Space (on by default): removes leading and trailing white space from the result.
- Fallback to Value: if the result cannot be read as the setting's type (for example
abcfor a number), the step uses the fixed value instead. It does not catch a script error or a missing global variable.
Some settings do not offer every preprocessor:
- The JavaScript Block of a Custom Step and of a Custom Event step offers only Global Variable. The script runs as written.
- The Start Portal Task Body and Card Config Body do not offer Liquid Template, because they are Liquid templates already (see below).
Runtime contract¶
| Item | Value |
|---|---|
| Engine | Liquid: DotLiquid 2.2, C# naming convention. Javascript: V8. |
| Runs when | Before the step starts, each time the step runs. |
| Timeout | 5 minutes per preprocessor. |
| Network access | None. |
| Objects in scope | Liquid: WorkItem. Javascript: workItem (alias WorkItem) and GlobalVariables. |
| Global variable version | The version current when the work item was last submitted, or when it entered the sub-workflow. |
The WorkItem model is the same in both. All metadata values are strings.
- Work item
WorkItemId,Name,Metadata[bucket][key], andDocuments[] - Each document
DocumentId,Metadata,Files[](Type,Name,Extension) andPages[](PageId,Index,Metadata,OriginFileName)
Secret variables
Javascript and Global Variable preprocessors can read secret variables. The value goes into the step setting: a request body, a URL, an email. Use a secret only in a setting that is meant to carry it, such as an authentication header, and never in a body, a URL query string or a message that people or other systems will see.
Liquid Template¶
Read values¶
{{ WorkItem.Name }}
{{ WorkItem.WorkItemId }}
{{ WorkItem.Metadata.Index.InvoiceNumber }}
{{ WorkItem.Metadata["Index"]["Invoice Total"] }}
- Property names use C# casing:
WorkItem.Name, notwork_item.name. - A key with a space or a dot needs the bracket form:
WorkItem.Metadata["Index"]["Invoice Total"]. - Bucket and key names are case-sensitive.
- A missing key renders as an empty string. Liquid errors are suppressed, so a mistake gives empty output, not an error. Check the result before you trust it.
Iterate documents¶
{% for doc in WorkItem.Documents %}
{{ doc.DocumentId }}: {{ doc.Metadata["Index"]["Vendor"] }}
{% endfor %}
Loops stop after 10,000 iterations.
Filters¶
DotLiquid has the standard Liquid filters. With the C# naming convention, write filter names in PascalCase:
{{ WorkItem.Metadata.Index.Vendor | Upcase }}
{{ WorkItem.Metadata.Index.InvoiceNumber | Replace: "-", "" }}
{{ WorkItem.Name | Truncate: 50 }}
{{ WorkItem.Metadata.Index.InvoiceDate | Date: "yyyy-MM-dd" }}
Common filters: Upcase, Downcase, Capitalize, Append, Prepend, Replace, Remove, Truncate, Size, Date (with a .NET date format), Plus, Minus, Times, DividedBy. Test a filter in a test workflow before you rely on it.
Liquid does not escape JSON
Liquid inserts values as they are. A value that holds a quote or a line break makes a JSON body invalid. For a JSON body, use the Javascript preprocessor with JSON.stringify, which always escapes correctly.
Javascript¶
The script's last expression is the setting's value.
- A string, number or boolean result becomes text, then the step reads it as the setting's type.
- For a list setting (a text array), return an array.
- For a JSON setting, such as the Call External API HTTP Headers, return a JSON string with
JSON.stringify. undefinedis an error: "Parameter script did not set a value".- Do not use a top-level
return. Wrap the logic in a function you call at once.
(function () {
var index = workItem.Metadata["Index"] || {};
return JSON.stringify({
workItemId: workItem.WorkItemId,
invoiceNumber: index["InvoiceNumber"] || "",
total: parseFloat(String(index["InvoiceTotal"] || "0").replace(/[^0-9.\-]/g, "")) || 0,
approved: (index["Approved"] || "").toLowerCase() === "true",
documentCount: workItem.Documents.length
});
})()
Global Variable¶
The setting takes the variable's value for this work item.
- List setting The variable must hold a JSON array of strings, such as
["a","b"]. - Structured setting For a setting with a structure, such as HTTP Headers, create the variable as an Activity Property for that step and setting, so it has the right shape.
- More See Global Variables.
Settings that are Liquid already¶
Some settings render Liquid themselves, with extra variables:
- Start Portal Task, Body (the email body):
{{ PortalTaskLink }}is the link to the task. The default is<a href="{{PortalTaskLink}}">Open Task</a>. - Start Portal Task, Card Config Body:
{{ DefaultCardBody }}inserts the standard card body. The default is{{DefaultCardBody}}.
Both also see WorkItem, for example PO Number: {{ WorkItem.Metadata.Trigger.PONumber }}. Their render timeout is 30 seconds.
Gotchas¶
Mistakes that bite
- The request body arrives with
{{ ... }}in it. The setting is on Value. Select the gear icon next to the setting's name and choose Liquid Template. - The value is empty. A bucket or key name is wrong, or has a different case. Liquid does not report it.
- The step fails with "Unable to read property". The preprocessor result does not parse as the setting's type. Fix the template, or turn on Fallback to Value.
- The JSON body is rejected with HTTP 400. A value contains a quote or a line break. Build the body with the Javascript preprocessor and
JSON.stringify. - "Parameter script did not set a value". The Javascript preprocessor's last expression was
undefined, often a function that was defined but not called. End with})(). - The value is from an old version of a global variable. Work items in flight keep the version from their submit time.
Related¶
- Call External API: the step that uses preprocessors most.
- Post Form Data to Your API and Handle Failures: a typed JSON body built with Javascript.
- Global Variables
- Routes