Customizing the Script widget

Last Updated:

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.

Important:

  • On September 17, 2022, Zoom Contact Center rolled out a new script engine that introduces a new syntax. Existing scripts will not be impacted. However, new script widgets must follow the new syntax. In this article, example scripts labeled as New version follow this new syntax.
  • To better understand usage of the Script widget, see example configurations.

This article covers:

Prerequisites for customizing the Script widget

How to add or edit the Script widget

  1. Sign in to the Zoom web portal.
  2. In the navigation menu, click Contact Center Management then Flows.
  3. Click a flow's display name to edit it.
  4. In the left-side widgets panel, click and drag Script onto the preferred location in the flow.
  5. Select the Script widget.
  6. (Optional) Click Rename to change the display name of the widget.
  7. 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.
  8. Click Add.
  9. Enter your JavaScript code.
  10. To test your code locally, on the right side, use the Input Parameters 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.
  11. Click Run.
    The result will appear in the Results section.
  12. Click Close.
  13. Click Save to save the script.
  14. (Optional) In the Settings tab, click these options:
    • Pencil icon : Edit the script.
    • Trash bin icon : Delete the script.
  15. 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 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.

Example of script mapping

See the account number example for details.

How to program scripts

  1. Add all dependencies to your code. See lines 2 to 4 in the example code below.
  2. 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

New version

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)

Old version

// Using Lambda

const log = require('./utils/log');
const req = require('axios'); // this is needed only if you plan to use Axios in your function
var {_get:var_get , _set:var_set, _setGlobalVariable:global_var_set} = require('./utils/variable');

module.exports = async () => {

// add your script logic. See two examples above.

return
}

Note: At this time, the only external module available in the Javascript Script widget is Axios.

How to use logging

Add the dependency import by using this function:

const log = require('./utils/log');

Note: For the new version of the script engine, this is build-in so you don’t need to import it again

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 implement HTTP requests using Axios. Refer to the Axios Support site for more information.

Add the dependency import by using this function:

const req = require('axios');

Note:

  • For the new version of the script engine, this is build-in so you don’t need to import it again
  • 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 and the Axios module to perform HTTP requests is available for more advanced use cases.

How to get variables within the Script widget

    1. Add and configure the Script widget in your flow.
    2. Create a JavaScript file and add the variable dependency import.

      Example:

      var {_get:var_get , _set:var_set, _setGlobalVariable:global_var_set}
            = require('./utils/variable');

Note: For the new version of the script engine, this is build-in so you don’t need to import it again

  1. Use the var_get function to get the value of the variable. The var_get function can access the value of both local widget and global variables. System global variables are defined by default, but custom global variables must be defined first.
    Example:

    var_get()["global_system.Consumer.firstName"]

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. In other words, if you set/change the value of a global variable within a flow, that value is only set for that specific engagement. If there are other consumers simultaneously or subsequently in the same flow, they will not have access to the updated global variable value set by another engagement. 

How to set local variables within the Script widget

  1. Add and configure the Script widget in your flow. For this example, rename the Script widget from Script to Script1.
  2. Create a JavaScript file and add the variable dependency import. Add the dependency import.
    Example:
    var {_get:var_get , _set:var_set, _setGlobalVariable:global_var_set} = require('./utils/variable');

    Note: For the new version of the script engine, this is build-in so you don’t need to import it again.

  3. Use the var_set function to set the value for a local variable. The first parameter is the full name of the local variable; the second parameter is the value.

    Example:
    var_set("account_number", “99999")
    The value of this variable can now be accessed within this flow as Script1.account_number

How to set global variables within the Script widget

  1. Add and configure the Script widget in your flow.

  2. Create a JavaScript file and add the variable dependency import.
    Example:
    var {_get:var_get , _set:var_set, _setGlobalVariable:global_var_set} = require('./utils/variable');

    Note: For the new version of the script engine, this is build-in so you don’t need to import it again

  3. 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.

Zoom Community

Join the 100K+ other members in the Zoom Community! Login with your Zoom account credentials and start collaborating.