Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Total tickets logged today be team

MunafbhaiU
Kilo Contributor

How to get total tickets logged today by Team? This should also capture the tickets that were created by Team A but now assigned to Team B. 

1 REPLY 1

Mohammed8
Mega Sage

I'm not sure you are asking for report or scripts but the use-case is pretty real and here is my take on it:

Using the background script helped verify two things. I used the Network team for testing, but the same logic works for any assignment group by using its sys_id.

  1. Which incidents were actually created today and are currently assigned to the Network team

  2. Whether the earliest assignment_group value in the audit history belonged to the Network team

Based on that, the script was able to correctly identify:

  • Tickets that were originally logged with the Network team

  • Tickets that were logged with the Network team but later reassigned to another team

var networkTeam = '287ebd7da9fe198100f92cc8d1d2154e'; // network team sys_id in my case, you check for any also

var currentNetwork = [];
var reassigned = [];
var seen = {}; // avoid duplicates

// STEP 1: Get all incidents created today
var inc = new GlideRecord('incident');
inc.addQuery('sys_created_on', '>=', gs.beginningOfToday());
inc.addQuery('sys_created_on', '<=', gs.endOfToday());
inc.query();

while (inc.next()) {

var sysId = inc.sys_id.toString();
var isCurrentNetwork = (inc.assignment_group.toString() == networkTeam);
var everAssignedNetwork = false;

// STEP 2: Check audit history → did NEW_VALUE ever equal NETWORK?
var audit = new GlideRecord('sys_audit');
audit.addQuery('tablename', 'incident');
audit.addQuery('documentkey', sysId);
audit.addQuery('fieldname', 'assignment_group');
audit.addQuery('oldvalue', networkTeam); // touched network at least once
audit.query();

if (audit.hasNext()) {
everAssignedNetwork = true;
}

// STEP 3: Categorize
if (isCurrentNetwork) {
// category 1: currently network
currentNetwork.push({
sys_id: sysId,
number: inc.number.toString(),
short_description: inc.short_description.toString(),
caller: inc.caller_id.getDisplayValue()
});
}
else if (everAssignedNetwork && !isCurrentNetwork && !seen[sysId]) {
// category 2: reassigned from network
seen[sysId] = true;

reassigned.push({
sys_id: sysId,
number: inc.number.toString(),
short_description: inc.short_description.toString(),
caller: inc.caller_id.getDisplayValue(),
current_group: inc.assignment_group.getDisplayValue()
});
}
}

// ===================== RESULTS ==========================

// CURRENT NETWORK
gs.print("========== CURRENTLY ASSIGNED Incident ==========");
gs.print("Total: " + currentNetwork.length);
gs.print("---------------------------------------------------");

for (var i = 0; i < currentNetwork.length; i++) {
var r = currentNetwork[i];
gs.print((i+1) + ". " + r.number +
" | Caller: " + r.caller +
" | " + r.short_description +
" | sys_id: " + r.sys_id);
}

// REASSIGNED
gs.print("\n========== REASSIGNED Incident ==========");
gs.print("Total: " + reassigned.length);
gs.print("---------------------------------------------------");

for (var j = 0; j < reassigned.length; j++) {
var x = reassigned[j];
gs.print((j+1) + ". " + x.number +
" | Caller: " + x.caller +
" | " + x.short_description +
" | Now in: " + x.current_group +
" | sys_id: " + x.sys_id);
}

 

Background Script Result:

 

Mohammed8_0-1764494206178.png

 Result Verification:

 

Mohammed8_1-1764494333772.png

 

Note:

In this use case we are checking just one day but the audit table is a heavy table, so this approach should only be used when the time window is small. 

 

If you find this answer useful, please mark it as solution accepted/helpful.

 

Thanks and Regards,

Mohammed Zakir