GlideRecord query() and while() to find out which records are missing

Nisar3
Giga Guru

Let's say I've the following code:

var gd = new GlideRecord('incident');
gd.addEncodedQuery('sys_idIN<sysid1,sysid2,sysid3,sysid4,sysid5>');
gd.query();
while(gd.next())
{
  gs.print(gd.number);
}

 

Now let's say sysid2 and sysid3 are not present in the system, gs.print() will only print 3 rows.

 

Now my question is, is there way to know after running this script which were missing in the system? I mean in this example it would be obvious since count is low but imagine I had 1000 sys_ids in the encodedQuery().

1 ACCEPTED SOLUTION

Ravi Gaurav
Giga Sage
Giga Sage

Hello, 

 

We need compares the list of sys_id values provided in the query with those actually found in the database. It then identifies and prints out which sys_id values from the original list are missing in the system.

Best approach is Array :-

var sysIds = ['sysid1', 'sysid2', 'sysid3', 'sysid4', 'sysid5']; // Array of sys_ids to check
var foundSysIds = []; // Array to store found sys_ids


var gd = new GlideRecord('incident');
gd.addEncodedQuery('sys_idIN' + sysIds.join(','));
gd.query();

while (gd.next()) {
foundSysIds.push(gd.sys_id.toString());
gs.print("Found: " + gd.number);
}var missingSysIds = sysIds.filter(function(id) {
return foundSysIds.indexOf(id) === -1;
});


if (missingSysIds.length > 0) {
gs.print("Missing sys_ids: " + missingSysIds.join(', '));
} else {
gs.print("No sys_ids are missing.");
}

try this and let me know 

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

View solution in original post

2 REPLIES 2

Ravi Gaurav
Giga Sage
Giga Sage

Hello, 

 

We need compares the list of sys_id values provided in the query with those actually found in the database. It then identifies and prints out which sys_id values from the original list are missing in the system.

Best approach is Array :-

var sysIds = ['sysid1', 'sysid2', 'sysid3', 'sysid4', 'sysid5']; // Array of sys_ids to check
var foundSysIds = []; // Array to store found sys_ids


var gd = new GlideRecord('incident');
gd.addEncodedQuery('sys_idIN' + sysIds.join(','));
gd.query();

while (gd.next()) {
foundSysIds.push(gd.sys_id.toString());
gs.print("Found: " + gd.number);
}var missingSysIds = sysIds.filter(function(id) {
return foundSysIds.indexOf(id) === -1;
});


if (missingSysIds.length > 0) {
gs.print("Missing sys_ids: " + missingSysIds.join(', '));
} else {
gs.print("No sys_ids are missing.");
}

try this and let me know 

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

Yes, this would work. I didn't want to go the array way but I guess to meet my requirement it will have to do. It was originally what I had thought too but posted this here in hopes that there would be any other way.