Not getting the source record of the finding in script-only check.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2022 12:56 AM
I have been trying to define a Script-Only check in the Instance Scan module of ServiceNow, which should show up all the update sets having duplicate names.
Below is the Script that I have written:
(function(engine) {
// Add your code here
var update_set = new GlideAggregate('sys_update_set');
update_set.addAggregate('COUNT');
update_set.query();
while (update_set.next()){
var no_of_records = update_set.getAggregate('COUNT');
var update_set_records = [];
update_set_records.push(update_set.getValue('name'));
}
var i = 0;
while (i < no_of_records) {
if (update_set_records[i] == update_set_records[i + 1]) {
engine.finding.setCurrentSource(update_set);
engine.finding.increment();
}
i++;
}
})(engine);
Here, after setting up the setCurrentSource('update_set'), I am getting the source table, but still not getting that exact record which satisfies the condition.
Please let me know if there is any other method under the engine or the finding object which specifies the record as well, or is there any other way to do that?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2022 03:33 AM
Issue might be due to the script. As you are using update_set as the glideRecord object for which the while loop has already ended.
Regards,
Sumanth

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2022 03:21 AM
Hi there,
Same issue as your previous question. It's not a Instance Scan issue. You just scripted this incorrectly. This would also not work correctly in background script.
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
2020-2022 ServiceNow Community MVP
2020-2022 ServiceNow Developer MVP
---
LinkedIn
Community article, blog, video list
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2022 03:28 AM
Hi there,
Can you help me pointing out the mistake in the script?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2022 04:23 AM
Try this script
(function (engine) {
function getDupes(theTable, dpField) {
var ga = new GlideAggregate(theTable);
ga.addAggregate('COUNT', dpField);
ga.addHaving('COUNT', dpField, '>', '1');
ga.query();
var arDupes = new Array();
while (ga.next()) {
arDupes.push(ga.getValue(dpField));
}
return arDupes;
}
var theTable = "sys_update_set";
var dpField = "name";
var strQuery = "nameIN" + getDupes(theTable, dpField);
var gr = new GlideRecord(theTable);
gr.addEncodedQuery(strQuery);
gr.query();
while (gr.next()) {
engine.finding.setCurrentSource(gr);
engine.finding.increment();
}
})(engine);
Mark as correct and helpful if it solved your query.
Regards,
Sumanth
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-13-2022 04:22 AM
Hi Sumanth,
Thanks for the solution. We are getting the correct source records after using this code.
But there is one more issue.
We are having 11 such records which have duplicate names. But we are getting 22 findings after setting the source with glideRecord. Like it's showing the same duplicate records in the findings as well.
Please help regarding this.
We have used the below code:
var arDupes = [];
function getDupes(theTable, dpField) {
var gaDupCheck = new GlideAggregate('sys_script_include');
gaDupCheck.addQuery('active', 'true');
gaDupCheck.addAggregate('COUNT', 'name');
gaDupCheck.addNotNullQuery('name');
gaDupCheck.groupBy('name');
gaDupCheck.addHaving('COUNT', '>', 1);
gaDupCheck.query();
while (gaDupCheck.next()) {
arDupes.push(gaDupCheck.name.getDisplayValue());
}
return arDupes;
}
var theTable = 'sys_script_include';
var dpField = 'name';
var strQuery = "nameIN" + getDupes(theTable, dpField);
var script_includes = new GlideRecord('sys_script_include');
script_includes.addEncodedQuery(strQuery);
script_includes.query();
while (script_includes.next()) {
finding.setCurrentSource(script_includes);
finding.increment();
}
}
Thanks