Write JavaScript
Last updated
Last updated
This action executes javascript code on a website.
Use a reference from a variable or a table:
Save a value to a variable or a table:
ref_id
and where to find it?ref_id
corresponds to the table id or variables id. In ZeroWork, tables and variables are considered data groups. Each data group has its own id (which is ref_id
), so ref_id
corresponds to the whole table id or to all variables. The individual columns and individual variables are uniquely identified by name.
Table ids can be found on the left sidebar:
Variables id can be found in the variables dialog:
Before moving on to examples, beware that getting table content adheres to ZeroWork dynamic loop behavior, as described below: Saving or Getting Data Within Loops.
Using browser in-built functions:
Making a fetch request, which returns an array and saving a value from the first item to a variable:
Both getRef()
and setRef()
are executed asynchronously. Make sure to wait for response, for example, by using await
. Otherwise the code will continue executing without waiting for response.
❌ Won't work
✅ Works
Variable and column names are case-sensitive. For example, if the column in your table is named "Profile Name" and you reference to it in lower case, you will get an error.
❌ Won't work
✅ Works
Ensure to use correct data types. ref_id
is expected to be number or string; name
and value
are expected to be strings.
❌ Won't work
✅ Works
✅ Also works
Whenever you use setRef()
, the value is stringified when it's saved. If the value is, for example, array or object and you need to access it later when calling getRef()
, you need to parse it first to json. See example below.
Assume you need to access the first item's object from the array saved to the variable "animals":
❌ Won't work
✅ Works
getRef()
and setRef()
adhere to ZeroWork in-built loop logic. For example, in order to access value from a table, you need to place Start Repeat building block with dynamic loop type before the Write JavaScript building block.
If you want to append data to a table, you need to place Start Repeat building block with standard loop type before the Write JavaScript building block.
You can append rows within a custom loop in your code block by providing an argument for appendIndex
.
Beware:
appendIndex
refers to the row number that should be appended. appendIndex: 0
means the first row to be appended. If there are already 3 rows in the table, it will be appended as 4th.
When using appendIndex
, avoid placing Write JavaScript building block inside any ZeroWork in-built loop (by this, Start Repeat building block is meant) - whether nested or not! Otherwise the row indices might get mixed up between ZeroWork in-built loop logic and your custom loop logic, and you might end up with mismatched data.
With that in mind, here is an example of how you can make this scenario work:
And here is the end result:
Custom looping through existing rows of a table within your code block in order to update data (as opposed to append) is currently not supported. However, in most use cases, adding dynamic loop with Start Repeat is enough to enable this use case. Please let us know if you want this feature to be implemented in our Discord server (#ideas-features-suggestions) while providing an example what you would need it for.
You can define your own log messages by using the function log
.
Will be shown in your logs like so:
You can include status
parameter, which is optional and accepts "success" or "fail". The default is "success". Must be either "fail" or "success".
Will be shown in your logs highlighted in red:
You can include tag
parameter, which is optional and makes your log message appear with a labeled tag in your logs. Must be string.
Will be shown in your logs with a tag on the right:
ZeroWork in-built error handling behavior will not always apply to your custom code. For example, if you use building block Click Web Element and the selector is not found, normally you will get an error that selector could not be found (after the corresponding timeout) in your logs and the TaskBot run will be interrupted. However, because browser console treats not found selectors differently (by simply returning null
), your TaskBot will continue its run despite the error.
❌ Not recommended
✅ Better
You can use custom error handling including custom try-catch logic and define custom behavior on error as well as your own custom error messages. Your custom error messages will be reflected in the logs and error reports. Consider the example below:
This is how your custom error message in the logs and error reports will appear:
Here is another example with your own custom try-catch logic:
getRef()
You might be used to inserting references directly from the dialog options. By this, we are referring to these options:
You can continue using these options. However, there are two caveats:
The values are replaced before the code is executed. If you need dynamic getting and setting behavior within your code, you need to use getRef()
and setRef()
.
Beware to not change the way references are pasted when using these options - for example, when using these in-built options, the name is not a string. This is intended and not to be confused with the arguments used in getRef()
.