How to get incident count in virtual agent chatbot

Pavan Kumar28
Tera Contributor

Hi All,

 

We are customizing the virtual agent topic for displaying the count of incident. So we have to get incidents which are updated additional comments within last 3days only. For this I tried one code but it fetching all the active incidents of caller but, I need incidents which are updated additional comments field  within last 3days.

Code:  This is oob script action utility code

(function execute() {
var incidents = new GlideAggregate('incident');
incidents.addQuery('active', true);
incidents.addQuery('caller_id', vaInputs.user);
incidents.addAggregate('COUNT');
incidents.query();
var incidentCount = 0;
if (incidents.next())
incidentCount = incidents.getAggregate('COUNT');
 
vaVars.incident_count = incidentCount;
})()

Output: getting all the active incidents of caller
PavanKumar28_0-1714017158940.png
 

regards,
Pavan.

1 ACCEPTED SOLUTION

Sujatha V M
Kilo Patron
Kilo Patron

@Pavan Kumar28 I tried to replicate your issue on the Incident table, 

 

ServiceNow does not allow filtering on journal fields directly from the list view. This is because journal fields are not stored in the same table as the record they are associated with. Instead, they are stored in a separate journal table (sys_journal_field), and each entry in a journal field is a separate record in this table.

 

Used a scripted filter to achieve the above use case,

 

1. Created a script include which holds the sys id of the incident as "Element ID"

 

SujathaVM_0-1714319209157.png

 

2. Create a filter which pushes the incidents records accordingly on the list view. 

 

Navigate to System Definition -> Filters -> Create New

 

SujathaVM_2-1714319421734.png

 

For testing purpose, I have updated the filter and it considered "Updated on as Today", change your filter accordingly. In the below screenshot it, lists only the record which got updated today. 

 

SujathaVM_3-1714321485677.png

 

3. In Virtual agent, in the script action update as below, 

Note: Encoded query line colon, 

incidents.addEncodedQuery(sys_id=javascript: new GetUpdatedIncs().getIncidents()^sys_updated_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday());

 

 

(function execute() {
    // Setting the incident count
    var incidents = new GlideAggregate('incident');
    incidents.addEncodedQuery('sys_id=javascript: new GetUpdatedIncs().getIncidents()^sys_updated_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()');
    //incidents.addQuery('active', true);
    //incidents.addQuery('caller_id', vaInputs.user);
    incidents.addAggregate('COUNT');
    incidents.query();
    var incidentCount = 0;
    if(incidents.next())
        incidentCount = incidents.getAggregate('COUNT');
    
    vaVars.incident_count = incidentCount;
})()

 

 

 

 

Results : "Displays 2 recently updated incidents" (text has been modified)

 

SujathaVM_4-1714323882338.png

 

 

Please mark this as helpful and accept it as a solution if this resolves your query.

Thanks,

Sujatha V.M.

 

Please mark this as helpful and accept it as a solution if this resolves your query.
Sujatha V.M.

View solution in original post

8 REPLIES 8

Hello @Sujatha V M,

I just modified the script as you suggested but, no luck getting same message "You have no active incidents". Could you pls help on this. Attaching the code output as snip.

Script Action code in virtual agent topic:

(function execute() {
// Setting the incident count
var incidents = new GlideAggregate('incident');
incidents.addEncodedQuery('sys_id=javascript: new GetAdditionalCommentsUpdatedtickets().getIncidents()^sys_updated_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()');
incidents.addQuery('active', true);
incidents.addQuery('caller_id', vaInputs.user);
incidents.addAggregate('COUNT');
incidents.query();
var incidentCount = 0;
if(incidents.next())
incidentCount = incidents.getAggregate('COUNT');
 
vaVars.incident_count = incidentCount;
})()


Output:

PavanKumar28_0-1714649643238.png

 


Thanks and regards,
Pavan.

@Pavan Kumar28  

 

As per the condition kindly make sure you have an incident updated today. 

 

Its a simple flow created for the use case with existing code that needs to be added to your topic. 

SujathaVM_0-1714657870284.png

 

I had updated an incident today where the caller field should the logged in user name. 

 

SujathaVM_1-1714658139076.png

 

Reference script was from the topic :  Check IT Ticket Status. 

 

SujathaVM_2-1714659949240.png

 


Please mark this as helpful and accept it as a solution if this resolves your query.

Thanks,

Sujatha V.M.

 

 

 

Please mark this as helpful and accept it as a solution if this resolves your query.
Sujatha V.M.

Hi @Sujatha V M ,

Thanks for your support, I just modified my output. Now it's working as expected.


Regards,
Pavan.

@Pavan Kumar28 Appreciate your efforts towards fixing it! Glad to help! 

 

Please mark this as helpful and accept it as a solution if this resolves your query.

Thanks,

Sujatha V.M.

Please mark this as helpful and accept it as a solution if this resolves your query.
Sujatha V.M.