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

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.

Hi @Sujatha V M,

Thanks for your info, I just tried the entire script which you are shared but i am getting "You have no active incidents" even though I updated additional comments for few Incidents. Did I miss something, can you please look on this. Is my filter condition is correct ?. Attaching the snip for the same.

Script Includes:

PavanKumar28_0-1714441306028.png

Filter Conditions:
Note: Sys ID is javascript:new GetAdditionalCommentsUpdatedtickets().getIncidents();

PavanKumar28_1-1714441366726.png

 


Script Action:

PavanKumar28_2-1714441591086.png

 

(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_3-1714441898754.png

 


Thanks and regards,
Pavan.

Hi @Sujatha V M,

Thanks for your info, I just tried the entire script which you are shared but i am getting "You have no active incidents" even though I updated additional comments for few Incidents. Did I miss something, can you please look on this. Is my filter condition is correct ?. Attaching the snip for the same.

Script Includes:

PavanKumar28_0-1714454539894.png

 

Filter Conditions:
Note: Sys ID is javascript:new GetAdditionalCommentsUpdatedtickets().getIncidents();

PavanKumar28_1-1714454540762.png

 

 


Script Action:

PavanKumar28_2-1714454539914.png

 

 

(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_3-1714454539933.png

 

 


Thanks and regards,
Pavan.

@Pavan Kumar28  Please rectify this line, &colon is the issue as it had reflected while copying. 

 

 

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

 

 

SujathaVM_0-1714479594591.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.