How to fix Virtual Agent script vaSystem.didConnectToLiveAgent() always returning false?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2022 01:06 PM
I am trying to add logic to a Virtual Agent topic block to handle the case where the end user requests a transfer to a live agent, agents are available, but no agents respond in their given timeline, effectively "timing out" the chat. Here's a screenshot of this scenario:
I would like instead of "no agent was available" to show a new user input, and continue on with my topic block (so that when no live agent accepts the call, the user is presented with options to "create a ticket", "call me back later", or "continue waiting").
Currently I am trying to use 3 methods (isLiveAgentAvailable, connectToAgent, and didConnectToLiveAgent) in a Script Action inside of a topic block. Documentation on these methods can be found here.
// option 1
if (vaSystem.isLiveAgentAvailable()) {
vaSystem.connectToAgent();
gs.info("Did live agent connect: " + vaSystem.didConnectToLiveAgent());
// this does not work. The message is immediately logged to the system with a value of false.
}
// option 2
if (vaSystem.isLiveAgentAvailable()) {
vaSystem.connectToAgent().then(function() {
gs.info("Did live agent connect: " + vaSystem.didConnectToLiveAgent());
// this does not work. The message is again immediately logged to the system with a value of false.
});
}
I tried a 3rd option where I moved the didConnectToLiveAgent() call to it's own scripted action, in the thinking that I would allow the connectToLiveAgent() call to complete first.
I have had no luck with any of these approaches - it seems that the value returned by didConnectToLiveAgent() is always false. Has anyone else run into this issue? Can you please advise on if this method works for you, or if you found another way to detect "timeouts" caused by live agents not answering in time?
- Labels:
-
Agent Chat
-
Virtual Agent

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2022 01:09 AM
Hi Spencer,
Just a thought but worth to try.
I am assuming that the vaSystem.connectToAgent(); method is asynchronous.
In this case your option1 does not work, because the vaSystem.connectToAgent(); method called, but the system does not wait for it to finish so it immediately jumps to the next line logging your info.
Option2 also does not work, because in San Diego all scripts are ES5, so no support for promises and the .then() notation.
I've tried in my PDI (where I do not have an agent set up) to see what is the difference.
First I've logged the value of the vaSystem.connectToAgent() method first then the value of the vaSystem.didConnectToLiveAgent() method second (similar to what you did in your option1) and they got logged in a different order (that supports the vaSystem.connectToAgent() is an asynchronous method assumption).
After that I did the same with a callback, and this time the order was ok.
function connectToAgent(callback) {
vaVars.agent = vaSystem.connectToAgent();
gs.info("--- " + vaVars.agent);
callback();
}
function checkConnection() {
gs.info("---Did live agent connect: " + vaSystem.didConnectToLiveAgent());
}
connectToAgent(checkConnection);
I hope it will help.
Richard
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2024 02:39 AM
Hi, I had the same issue of using the vaSysyem.didConnectToLiveAgent() and could not make this to show 'true' when I used vaSystem.connectToAgent().
I have spoken to ServiceNow about this as I could not find any description of the method usage.
The Information I got from ServiceNow allowed me to use the .didConnectToLiveAgent() method properly and reach my target.
The clue is that: to be able to retrieve the correct return value, 'connectToAgent' and 'didConnectToLiveAgent' functions need to be in different Topics.
You may for example create a new Closing Conversation Topic and pin it to your Virtual Agent instead of the default one.
In your Custom Closing Conversation topic, that is running always after the VA conversation is finished (either successfully connected or not - for example when the call is rejected or is timed out) you may put additional script and put for example gs.log (aSystem.didConnectToLiveAgent()) which should give you log entry 'true', when there was chat with agent or 'false' if not.
I have tried that and it worked great. I could then set the rest of logic based on the result, that is redirection to the other topic (if the method returned false) or end the conversation as the Custom Closing Conversation was initially set to.
I hope that this helps to anyone who has the issue with using this method.
Please mark this answer as helpful if you find it so:)
Of course feel free to comment that, I am opened for discussions on this.
Thanks,
Bartosz
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2025 07:53 AM - edited 04-23-2025 07:54 AM
To piggy back on this solution, here is a more instructive way to implement this with images.
Duplicate the out of the box 'Closing conversation.' topic.
Create a global topic variable. I called mine 'agentConnection'
Insert your script in the newly created closing conversation with a decision block.
The 'agentConnection' script block below contains the following script:
(function execute() {
vaVars.agentConnection = vaSystem.didConnectToLiveAgent();
})()
The branching options for the decision contains the following scripts:
For agentConnection true:
(function execute() {
var applies = vaVars.agentConnection == true ? true : false;
return applies;
})()
For agentConnection false:
(function execute() {
var applies = vaVars.agentConnection == false ? true : false;
return applies;
})()
Here is a snip of the topic design:
I created a simple bot text within the agentConnection false branch to verify the value is returning properly:
Next, go to Conversational Analytics > Virtual Agent > Custom Greetings & Setup > Chat experience
Select Setup Topics
Select 'Closing'
Replace the value in the 'Setup topic' field with your Custom Closing Conversation.
Once the wait time for the selected queue expires, here is the result: