getVariable() and setVariable(), act as the bridge between your JavaScript code and the rest of your Plural flow.
Add a Custom Script element
Right-click on an empty area of your canvas and select Custom Script. Click the element once to open the sidebar, then click Open script editor to launch the full-screen code editor.The getVariable() function
Use getVariable() to read the current value of any flow variable (custom or system) into your script. The function takes the variable name as a string and returns its value.
The setVariable() function
Use setVariable() to write a value back to a flow variable. The function takes two arguments: the variable name and the new value.
#ATTRI/variableName.
Script outputs
The Custom Script element has two outputs:- Success — the script ran without throwing an error.
- Error — an unhandled JavaScript error occurred (for example, a
TypeErrororReferenceError).
Example: Build a simple counter
This example builds a flow counter that users can increment or decrement with buttons, and displays the current count on demand.Overview of the flow
- A Set User Attributes & Variables element initialises
counterto0. - A Menu Designer element presents three buttons: Count Up, Count Down, and Show Count.
- Two Custom Script elements handle the increment and decrement logic.
- A Robot Says element reads back the current count using
#ATTRI/counter.
Step 1 — Initialise the counter
Add a Set User Attributes & Variables element at the start of your flow. Create a variable namedcounter with the value 0.
Step 2 — Create the menu
Add a Menu Designer element with three buttons: Count Up, Count Down, and Show Count. Wire the Show Count output to a Robot Says element that speaks"The current count is #ATTRI/counter".
Step 3 — Add the increment script
Connect the Count Up button output to a Custom Script element. Open the script editor and enter:getVariable('counter')reads the current counter value from the flow into the local variablecurrentCounter.parseInt(currentCounter) + 1converts the string to an integer and adds 1 — withoutparseInt, JavaScript would concatenate strings instead of adding numbers.setVariable('counter', newCounter)saves the updated value back so every subsequent element can see it.
Step 4 — Add the decrement script
Connect the Count Down button output to a second Custom Script element. Duplicate the increment script and change+ 1 to - 1:
