Can we dynamically show "position in queue" to user in virtual agent

dineshchoudhary
Kilo Guru

I have configured the queue count during a live agent transfer in the Virtual Agent and displayed it as a system message using the script below. However, I am looking for options to dynamically track and update this count in real time. Is there a way to achieve this?

 

(function execute() {
        var VALq = new GlideAggregate('awa_interaction_work_item');
        VALq.addEncodedQuery("wi_active=true^wi_state=queued");
        VALq.addAggregate('COUNT');
        VALq.query();
        if (VALq.next()) {
            var countAgent =  VALq.getAggregate('COUNT');
        }    
        var msg = "Your current queue position is: " + countAgent; 
        vaSystem.sendSystemMessage(msg)
})()

dineshchoudhary_0-1742946308481.png

 

dineshchoudhary_1-1742946308534.png

2 REPLIES 2

Keszia
Giga Guru

I would love to know this too, as someone has just asked me if this is possible. I looked at your script and Copilot suggested the following instead (I'm not proficient with coding hence my reliance on AI....):

----

To show the user's queue position: You need to:

  1. Query all active, queued work items, sorted by creation (e.g., sys_created_on).
  2. Loop through them and find the position of the current user's work item (using a unique identifier, e.g., sys_id or user reference).

Example (assuming you have the current work item's sys_id as currentSysId):

(function execute() {
    var currentSysId = /* set this to the current work item's sys_id */;
    var gr = new GlideRecord('awa_interaction_work_item');
    gr.addEncodedQuery("wi_active=true^wi_state=queued");
    gr.orderBy('sys_created_on');
    gr.query();

    var position = 1;
    var found = false;
    while (gr.next()) {
        if (gr.getValue('sys_id') == currentSysId) {
            found = true;
            break;
        }
        position++;
    }

    var msg;
    if (found) {
        msg = "Your current queue position is: " + position;
    } else {
        msg = "Your work item is not currently in the queue.";
    }
    vaSystem.sendSystemMessage(msg);
})();

Note:

  • Replace currentSysId with the actual sys_id of the user's work item.
  • This script will send the user's actual position in the queue, not just the total count.

Keszia
Giga Guru

Also, where are you adding this script in the topic in relation to the transfer to live agent script?