Building a Database Lookup in NICE CXone Studio with DBConnector
What You Will Build
- A CXone Studio workflow that intercepts an inbound interaction, extracts a unique identifier, and queries an external PostgreSQL database to retrieve customer loyalty points.
- This tutorial utilizes the NICE CXone Studio visual workflow engine and the specific
DBConnectoraction node. - The implementation covers Studio JSON configuration, SQL parameterization, and error handling within the workflow context.
Prerequisites
- NICE CXone Access: You must have access to the CXone Studio environment with permissions to create and publish workflows.
- Database Connectivity: A PostgreSQL, MySQL, or SQL Server instance that is accessible from the NICE CXone network. If the database is on-premise, you must have the NICE CXone On-Premise Connector installed and configured.
- Studio Skills: Familiarity with the CXone Studio interface, specifically the “Actions” palette and the “Data” panel.
- SQL Knowledge: Basic understanding of
SELECTstatements and parameterized queries to prevent injection attacks.
Authentication Setup
CXone Studio workflows run in a serverless execution context. You do not write code to authenticate with CXone; the platform handles the session. However, the DBConnector action requires credentials to connect to your external database.
- Navigate to Integrations > Connectors in the CXone Admin Console.
- Create a new Database Connector.
- Select your database type (e.g., PostgreSQL).
- Enter the Host, Port, Database Name, Username, and Password.
- Test the connection.
- Note the Connector ID. You will need this ID in the Studio workflow configuration.
Note: Never hardcode database credentials in Studio variables. Always use the Connector object.
Implementation
Step 1: Configure the Studio Workflow and Input Variables
Before adding the database action, you must define the input data that will be sent to the database. In this example, we assume an inbound call where the caller provides their Customer ID via DTMF or IVR.
- Open CXone Studio.
- Create a new Workflow.
- Add an Input node or use the existing
Interactioncontext. - Create a variable named
customer_idof typeString. - Map this variable to the relevant source (e.g.,
Interaction.Caller.PhoneNumberor a custom IVR variable).
In Studio, variables are accessible in the JSON configuration using the syntax {{variable_name}}. For the DBConnector, we will map this variable to a SQL parameter.
Step 2: Add and Configure the DBConnector Action
This is the core step. We will add the action that performs the actual database query.
- Drag the DBConnector action from the Actions palette onto the canvas.
- Connect it to the previous node (e.g., the Input node or a Set Variable node).
- Click on the DBConnector node to open the Properties panel.
Configuring the Connection
- Connector: Select the Database Connector you created in the Prerequisites section.
- Operation: Select
Query(for SELECT statements). Do not useExecutefor read-only lookups unless necessary, asQueryreturns result sets more efficiently.
Configuring the SQL Statement
You must write a parameterized SQL query. Never use string concatenation (e.g., SELECT * FROM users WHERE id = '{{customer_id}}') in the SQL field. This is a security risk and will fail if the ID contains special characters.
In the SQL field, enter:
SELECT points, tier_name
FROM customer_loyalty
WHERE customer_id = ?
LIMIT 1
Note: The syntax for parameters (?) depends on the database driver. PostgreSQL and MySQL typically use ? or $1. SQL Server uses @param. The CXone DBConnector abstracts this slightly, but using ? is the safest standard for the visual config.
Mapping Parameters
In the Parameters section of the Properties panel:
- Click Add Parameter.
- Set Parameter Name to
param_1(or match the driver expectation). - Set Value to
{{customer_id}}. - Set Data Type to
StringorIntegerdepending on your schema.
Configuring Output
The DBConnector returns a result set. By default, it may return a JSON array.
- In the Output section, set the Result Variable to
db_result. - Ensure Return Type is set to
JSONorList.
Step 3: Process the Result and Handle Empty Sets
The database query might return no rows if the customer ID does not exist. You must handle this gracefully to prevent the workflow from breaking.
- Add a Condition node after the DBConnector.
- Configure the condition:
- If
{{db_result}}is not empty (or{{db_result.count}} > 0).
- If
- In the True branch:
- Add a Set Variable node.
- Create a variable
loyalty_points. - Set Value to
{{db_result[0].points}}. - Create a variable
tier_name. - Set Value to
{{db_result[0].tier_name}}.
- In the False branch:
- Add a Set Variable node.
- Set
loyalty_pointsto0. - Set
tier_nametoUnknown.
Step 4: Validate the Data Structure
CXone Studio variables are dynamic. If the database column points is an integer, Studio will treat it as a number. If it is NULL in the database, Studio may treat it as null or an empty string.
Add a Script node (JavaScript) after the Condition node to sanitize the data. This ensures downstream nodes (like TTS or Email) receive valid strings.
Script Code:
// Input: loyalty_points (number/null), tier_name (string/null)
// Output: safe_points (string), safe_tier (string)
let rawPoints = input.loyalty_points;
let rawTier = input.tier_name;
// Handle null or undefined from database
let safePoints = (rawPoints !== null && rawPoints !== undefined) ? rawPoints.toString() : "0";
let safeTier = (rawTier !== null && rawTier !== undefined) ? rawTier.toString() : "Valued Member";
output.safe_points = safePoints;
output.safe_tier = safeTier;
Complete Working Example
Below is the conceptual JSON representation of the Studio Workflow configuration. While you configure this visually, understanding the underlying JSON helps when debugging complex mappings.
{
"workflow": {
"id": "wf_db_lookup_example",
"name": "Customer Loyalty Lookup",
"nodes": [
{
"id": "node_input",
"type": "Input",
"properties": {
"variables": [
{
"name": "customer_id",
"type": "string",
"source": "interaction.caller.phoneNumber"
}
]
},
"outgoing": ["node_db_lookup"]
},
{
"id": "node_db_lookup",
"type": "DBConnector",
"properties": {
"connectorId": "conn_postgres_01",
"operation": "Query",
"sql": "SELECT points, tier_name FROM customer_loyalty WHERE customer_id = ?",
"parameters": [
{
"name": "param_1",
"value": "{{customer_id}}",
"dataType": "String"
}
],
"resultVariable": "db_result",
"timeout": 5000
},
"outgoing": ["node_check_result"]
},
{
"id": "node_check_result",
"type": "Condition",
"properties": {
"conditions": [
{
"operator": "IsNotEmpty",
"value": "{{db_result}}"
}
]
},
"outgoing": {
"true": "node_set_found",
"false": "node_set_not_found"
}
},
{
"id": "node_set_found",
"type": "SetVariable",
"properties": {
"assignments": [
{
"variable": "loyalty_points",
"value": "{{db_result[0].points}}"
},
{
"variable": "tier_name",
"value": "{{db_result[0].tier_name}}"
}
]
},
"outgoing": ["node_finalize"]
},
{
"id": "node_set_not_found",
"type": "SetVariable",
"properties": {
"assignments": [
{
"variable": "loyalty_points",
"value": "0"
},
{
"variable": "tier_name",
"value": "New Customer"
}
]
},
"outgoing": ["node_finalize"]
},
{
"id": "node_finalize",
"type": "Script",
"properties": {
"language": "javascript",
"code": "output.final_points = input.loyalty_points.toString();"
},
"outgoing": ["node_end"]
},
{
"id": "node_end",
"type": "End",
"properties": {}
}
]
}
}
Common Errors & Debugging
Error: DBConnector Timeout
- What causes it: The database query takes longer than the configured timeout (default is often 5-10 seconds). This happens with unindexed columns or large result sets.
- How to fix it:
- Check the SQL execution plan in your database. Ensure
customer_idis indexed. - Increase the timeout in the DBConnector properties (e.g., to 15000 ms).
- Add
LIMIT 1to your SQL if you only expect one row.
- Check the SQL execution plan in your database. Ensure
Error: “Parameter type mismatch”
- What causes it: The data type defined in the Studio Parameter mapping does not match the database column type. For example, sending a String
?to an Integer column. - How to fix it:
- In the DBConnector properties, change the Data Type of the parameter to match the database schema (e.g., change from
StringtoInteger). - Alternatively, cast the variable in a preceding Script node:
output.int_id = parseInt(input.customer_id);and pass{{int_id}}to the DBConnector.
- In the DBConnector properties, change the Data Type of the parameter to match the database schema (e.g., change from
Error: “Result set is empty” when data exists
- What causes it: The
customer_idvariable contains leading/trailing whitespace or case sensitivity issues. - How to fix it:
- Add a Set Variable node before the DBConnector to clean the input:
output.clean_id = trim(input.customer_id). - Use
LOWER()in your SQL:WHERE LOWER(customer_id) = LOWER(?).
- Add a Set Variable node before the DBConnector to clean the input:
Error: “Connector not found”
- What causes it: The Connector ID referenced in the workflow does not exist or is not published.
- How to fix it:
- Go to Integrations > Connectors.
- Verify the connector status is Active.
- Ensure you are selecting the correct connector from the dropdown in Studio.