const { Variables } = require("camunda-external-task-client-js");
// ... somewhere in the handler function
const variables = new Variables().setAll({ foo: "some foo value" });
console.log("foo", variables.get("foo"));
Note: All setters return the variables instance. This can be helpful for chaining calls.
The following option can be passed but is not required:
Option | Description | Type |
---|---|---|
readOnly | If set to true, only getters functions are available. | boolean |
Returns the value of the variable with key variableName.
// Given:
// score: { value: 5, type: "integer", valueInfo: {} }
const score = variables.get("score");
console.log(score);
Output:
5
Returns the typed value of the variable with key variableName.
// Given
// score: { value: 5, type: "integer", valueInfo: {} }
const score = variables.getTyped("score");
console.log(score);
Output:
{ value: 5, type: "integer", valueInfo: {} }
A typed value is an object with the following structure:
{
value: "some value",
// type of the variable, e.g. integer, long, string, boolean, json ...
type: "string",
// An object containing additional, value-type-dependent properties
valueInfo: {}
}
Returns the values of all variables.
// Given:
// {
// score: { value: 5, type: "integer", valueInfo: {} }
// isWinning: { value: false, type: "boolean", valueInfo: {} }
// }
const values = variables.getAll();
console.log(values);
Output:
{ score: 5, isWinning: false }
Returns the typed values of all variables.
// Given:
// {
// score: { value: 5, type: "integer", valueInfo: {} }
// isWinning: { value: false, type: "boolean", valueInfo: {} }
// }
const typedValues = variables.getAllTyped();
console.log(typedValues);
Output:
{
score: { value: 5, type: "integer", valueInfo: {} },
isWinning: { value: false, type: "boolean", valueInfo: {} }
}
Sets a value for the variable with key variableName.
Note: The variable type is determined automatically.
variables.set("fullName", { first: "John", last: "Doe" });
console.log(variables.getTyped("fullName"));
Output:
{
value: { first: "John", last: "Doe" },
type: "json",
valueInfo: {}
}
Sets a value for the variable with key variableName, also sets transient flag true to variable.
Note: The variable type is determined automatically.
variables.setTransient("fullName", { first: "John", last: "Doe" });
console.log(variables.getTyped("fullName"));
Output:
{
value: { first: "John", last: "Doe" },
type: "json",
valueInfo: {transient: true}
}
Sets a typed value for the variable with key variableName
Note: The variable type is not case sensitive.
variables.setTyped("test", {
value: "<test id=2 />",
type: "XML",
valueInfo: {}
});
console.log(variables.getTyped("test"));
Output
{
value: "<test id=2 />",
type: "XML",
valueInfo: {}
}
Sets the values of multiple variables at once.
// Given:
// {
// score: { value: 6, type: "integer", valueInfo: {} }
// isWinning: { value: true, type: "boolean", valueInfo: {} }
// }
variables.setAll({
score: 8,
message: "Score is on 🔥"
});
console.log(variables.getAll());
Output:
{ score: 8, isWinning: true, message: "Score is on 🔥" }
Sets the typed values of multiple variables at once.
// Given:
// {
// score: { value: 6, type: "integer", valueInfo: {} }
// isWinning: { value: true, type: "boolean", valueInfo: {} }
// }
variables.setAllTyped({
score: { value: 8, type: "short", valueInfo: {} },
message: { value: "Score is on 🔥", type: "string", valueInfo: {} }
});
console.log(variables.getAllTyped());
Output:
{
score: { value: 8 , type: "short", valueInfo: {} },
isWinning: { value: true, type: "boolean", valueInfo: {} },
message: { value: "Score is on 🔥" , type: "string", valueInfo: {} },
}
Date and JSON values are automatically serialized when being set and deserialized when being read.
// 'variables.set()' can be used to set a date by providing a date object
variables.set("someDate", new Date());
// 'variables.setTyped()' can be used to set a date by either:
// 1- providing a date object as a value:
variables.setTyped("anotherDate", { type: "date", value: new Date(), valueInfo: {} });
// 2- providing a date string as a value:
variables.setTyped("anotherDate", { type: "date", value: "2016-01-25T13:33:42.165+0100", valueInfo: {} });
// `variables.get("anotherDate")` is a date object
console.log(typeof variables.get("anotherDate")); // output: object
// `variables.getTyped("anotherDate").value` is date object
console.log(typeof variables.getTyped("anotherDate").value); // output: object
// 'variables.set()' can be used to set a JSON object by providing an object
variables.set("meal", { id: 0, name: "pasta" });
// The same is also possible with `variables.setTyped()`
variables.setTyped({
type: "json",
value: { id: 0, name: "pasta" },
valueInfo: {}
});
// `variables.get("meal")` is an object
console.log(variables.get("someJSON")); // output: { id: 0, name: "pasta" }
// `variables.getTyped("meal").value` is an object
console.log(variables.getTyped("someJSON").value); // output: { id: 0, name: "pasta" }