When you insert the Script Component, you will see an area where you can add a description and an “Edit Code” button. Clicking on this button will open a code editor with a dark-themed workspace where you can write and save your JavaScript code.
Below are some examples that illustrate how to use the component:
You can access context variables using the context object. For example:
var myVar1 = context.getValue("myVar1");
var myVar2 = "Hello World " + myVar1;
context.setValue("myVar2", myVar2);
The Script component also allows you to define and use custom functions within your script:
function validate(value) {
return value > 0;
}
function validateVariables() {
var myVar1 = context.getValue("myVar1");
if (!validate(myVar1)) {
return "Invalid variable myVar1";
}
var myVar2 = context.getValue("myVar2");
if (!validate(myVar2)) {
return "Invalid variable myVar2";
}
return "All variables are valid";
}
context.setValue("validationResults", validateVariables());
For debugging purposes, you can use the console object to add logs:
console.log("Hello World Script!");
console.log(2+2);
The execution of this script will display the following output in the console:
Hello World Script!
4