Customizing the Script widget
Contact center admins can add and edit the Script widget. The Script widget allows you to perform specific actions in the flow so that you don't need to rely on the UI to build flows. Keys, secrets, and other credentials are hidden by default in Script widgets.
Requirements for customizing the Script widget
How to add or edit the Script widget
- Sign in to the Zoom web portal as an admin with the privilege to edit flow settings.
- In the navigation menu, click Contact Center Management then Flows.
- Click a flow's display name to edit it.
- In the left-side widgets panel, click and drag Script onto the preferred location in the flow.
- Select the Script widget.
- (Optional) Click Rename to change the display name of the widget.
- Click Add Script to add a new script to the widget, then specify these options:
- Name: Enter an internal display name to help identify the script.
- Language: Select the scripting language.
- Click Add.
- Enter your JavaScript code.
- To test your code locally, on the right side, use the Input Parameter panel to input parameters for the script in JSON format. Make sure to include the appropriate key/value pairs as required by your JavaScript code. When testing your code locally, you will need to provide all expected key/value pairs as Input Parameters. For example, if you need to pass the ANI into your Script for testing, you can use the JSON object {"global_system.Engagement.ANI":"+16505551234"}
- Click run.
The result will appear in the Result section. - Click Close.
- Click Save to save the script.
- (Optional) In the Settings tab, use these options:
- Pencil icon : Edit the script.
- Trash bin icon : Delete the script.
- Output variables: Click Add Output then select a local or global variable in the Script.value drop-down menu.
- Click the Exits tab to customize exit settings.
- Success: Select the destination widget if the script is able to run successfully.
- Fail: Select the destination widget if the script failed to run.
Note: To better understand usage of the Script widget, see example configurations.
Script variable mapping
After configuring the Script widget and running it, you can map the script variable values to a flow variable.
- For all variables set by var_set in script, it is set into a flow variable under the widget name. This can be considered a local widget variable.
- For the variable returned by the script, it is set into a flow variable called value under the widget name.
- For all variables set by global_var_set, these variables should be defined using global custom variables. These global variables can be referenced by the full global variable name and modified in the Script widget using global_var_set.
You can also map variables in the Output variables section when customizing the Script widget.
Example of script mapping
See the account number example for details.
How to program scripts
- Add all dependencies to your code. See lines 2 to 4 in the example code below.
- Pass your function to module.exports so that code engine can extract your JS function. Use async only when you need await in your function. There is no need to call the function within the code panel.
Note: Strict mode is turned on automatically.
Example of script programming
async function main () {
// your code starts here
}
Note:
#### Supported built-in functions: (no need to import again)
## Variable Get/Set:
var_get()[string]; var_set(string, string); global_var_set(string, string)
## Http Requests:
req.get(url[, config]); req.delete(url[, config]); req.head(url[, config]); req.options(url[, config])
req.post(url[, data[, config]]); req.put(url[, data[, config]]); req.patch(url[, data[, config]])
## Logging
log.debug(string), log.info(string), log.warn(string), log.error(string)
Note: At this time, only the built-in functions above are available. External modules are not supported.
How to use logging
You can log information in your scripts by leveraging the 'log' built-in function as shown below. To view the logs, you will need to enable the Flow debug console.
Example of logging
You can implement logging by using these functions:
log.debug("debugging enabled")
log.error("error detected")
log.info("info: yes")
log.warn("WARNINGS: ")
How to implement HTTP requests
You can send HTTP requests using the Script widget. The Script widget javascript code provides you the ability to send HTTP requests using the built-in function 'req'.
Note: You can implement HTTP requests using the standard Http Call widget which allows you to perform HTTP requests without writing JavaScript functions. However, using a Script widget and the 'req' built-in function to perform HTTP requests is available for more advanced use cases.
Example of HTTP request
Below is an example of an HTTP request.
const response = await req.get("https://api.example.com")
Refer to the Axios Support site for more information.
How to get variables within the Script widget
You can get variable information in your scripts by leveraging the 'var_get' built-in function as shown below. A common use case for this is to read global variable data into your script code, and then perform action based on the variable values. To do this, you will read a global variable and temporarily access/store it in your script.
Use the var_get function to get the value of the variable. The var_get function can access the value of both local and global variables.
Example for reading Global System variable:
let first_name = var_get()["global_system.Consumer.firstName"]
Example for reading Global Custom variable:
let account_number = var_get()['global_custom.General.account_number']
Note: System global variables are defined by default, but global custom global variables must be defined first. In the above Global Custom variable example, a custom variable group named "General" was created and a custom global variable named "account_number" was created in the group.
Note: For more information on global variables, including whether values are shared across multiple engagements, please consult the global variables and review the difference between "Linked Value", "Default Value", and "Undefined" variable types.
How to set local variables within the Script widget
You can set local variable values in your scripts by leveraging the 'var_set' built-in function as shown below. A common use case for this is to store some data generated in your script (e.g. API response) into a local variable.
- Add and configure the Script widget in your flow. For this example, rename the Script widget from Script to Script1.
- Use the var_set function to set the value for a local widget variable. The first parameter is the full name of the local widget variable; the second parameter is the value.
Example:var_set("account_number", “99999")
The value of this variable will now be accessible within this flow as Script1.account_number - Alternatively, map the local widget variable on the exit of the Script widget. Return to the Script widget, and click on the Add Output link.
- In the Script Variable field, enter the name of the variable. For this example, it's "account_number".
- In the Map output to dropdown, select a local flow variable or global variable for mapping.
- If your script has multiple var_set functions to store variables, repeat steps 4-6 for each variable.
How to set global variables within the Script widget
You can set global variable values in your scripts by leveraging the 'global_var_set' built-in function as shown below. A common use case for this is to store some data generated in your script (e.g. API response) into a global variable, which can then be used in ZCC logs and/or displayed to the Contact Center agent.
- Add and configure the Script widget in your flow.
- Use the global_var_set function to set the value for a global variable. The first parameter is the full name of the global variable; the second parameter is the value.
Example:
global_var_set("global_system.Consumer.firstName", "Your Name")
Note: While global variables are defined globally on the system, any value that you set in a global variable within a flow is limited in scope to the active engagement.
Important:
- On September 17, 2022, Zoom Contact Center rolled out a new script engine that introduces a new syntax. Existing scripts created prior to this data are still supported, however they will use legacy code syntax that is not covered in this article. For any customers using legacy script code, that was created prior to this date, we request that you update the code to the newer syntax as shown below.
- To better understand usage of the Script widget, see example configurations.