Chat JS workflows
Customer’s ID is prepopulated on the webchat by the embedding site. This ID is then used to assign the chat to a record. If no record is found, then a new one is created. Afterwards, the chat is moved to a queue.
function chatStarted(ctx) {
// Your code here, for example:
console.log("Chat started!");
vcc.setOnMessage(saveCustomerAndAssignChat);
}
// Function to save customer and assign the chat
function saveCustomerAndAssignChat(ctx) {
var clientid = vcc.getVariable('clientid');
// Search for an existing customer record by clientId
var records = vcc.searchCustomer("client_id", clientid);
if (records && records.rows.length > 0) {
// If the customer exists, update their record and assign the chat
vcc.assignCustomer(records.rows[0].id, 0); // Assign to the first contact (ID 0)
addToQueue(ctx);
return;
}
// If customer doesn't exist, create a new customer
var newCustomerId = vcc.createCustomer([{
"form": {
"client_id": clientid
}
}]);
// If the new customer is created successfully, assign the chat
if (newCustomerId != 0) {
vcc.assignCustomer(newCustomerId, 0); // Assign to the first contact (ID 0)
addToQueue(ctx);
return;
}
}
// Function to add to the queue if no response within timeout
function addToQueue(ctx) {
vcc.addToQueue({queueId: 1}); // Add the chat to queue with ID 3803
console.log("No response in time, added to queue.");
}
In this scenario, the system asks the customer to provide a Customer ID manually. This ID is then used to assign the chat to a record. If no record is found, then a new one is created. Afterwards, the chat is moved to a queue.
function chatStarted(ctx) {
// Your code here, for example:
console.log("Chat started!");
vcc.setOnMessage(askCustomerId);
}
function askCustomerId(ctx) {
// Asking for Customer ID
vcc.askVariable({
nextAction: saveCustomerAndAssignChat, // This function will be triggered after the question
variable: "customerid", // Variable name to store the response
question: "What is your Customer ID?", // The question to ask
// timeout: 30, // Wait for 30 seconds before timing out
// timeoutAction: addToQueue // Action to take if the timeout expires
});
}
// Function to save customer and assign the chat
function saveCustomerAndAssignChat(ctx) {
var customerid = vcc.getVariable('customerid');
// Search for an existing customer record by clientId
var records = vcc.searchCustomer("customerid", customerid);
if (records && records.rows.length > 0) {
// If the customer exists, update their record and assign the chat
vcc.assignCustomer(records.rows[0].id, 0); // Assign to the first contact (ID 0)
addToQueue(ctx);
return;
}
// If customer doesn't exist, create a new customer
var newCustomerId = vcc.createCustomer([{
"form": {
"customerid": customerid
}
}]);
// If the new customer is created successfully, assign the chat
if (newCustomerId != 0) {
vcc.assignCustomer(newCustomerId, 0); // Assign to the first contact (ID 0)
addToQueue(ctx);
return;
}
}
// Function to add to the queue if no response within timeout
function addToQueue(ctx) {
vcc.addToQueue({queueId: 1}); // Add the chat to queue with ID 3803
console.log("No response in time, added to queue.");
}
In this scenario, the system asks the customer for a phone number and then creates a record in the project with a shared callback disposition. The customer is identified by a clientid, which is passed at initiation.
// Function to handle chat start
function chatStarted(ctx) {
console.log("Chat started!");
// Set the onMessage handler to proceed with the conversation
vcc.setOnMessage(askPhoneNumber);
}
function askPhoneNumber() {
vcc.askVariable({
variable: "userPhoneNumber",
question: "Please enter your phone number to receive your exclusive offer:",
nextAction: finishPromoFlow,
timeout: 300,
timeoutAction: finishPromoFlow
});
}
// Function to save customer and assign the chat
function finishPromoFlow(ctx) {
var clientid = vcc.getVariable('client_id');
var userPhoneNumber = vcc.getVariable('userPhoneNumber');
// Create a new customer
var newCustomerId = vcc.createCustomer([{
"form": {
"client_id": clientid
},
"contacts": {
"1": {
"phone": userPhoneNumber,
}
},
"disposition": {
"dispositionid": "2",
"next_calldate": "2026-07-20 10:26:55"
}
}]);
finishFlow(ctx);
}
// Function to add to the queue if no response within timeout
function finishFlow(ctx) {
vcc.sendMessage("Thank you, our colleague will be in touch with you soon!");
console.log("No response in time, added to queue.");
}
Comments
Can’t find what you need? Use the comment section below to connect with others, get answers from our experts, or share your ideas with us.
There are no comments yet.