COUNT DISTINCT by dot-walking filed in PA script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2025 12:59 AM
Hi All,
I created the PA indicator, need to COUNT DISTINCT by Change request which associate from Outage --> Incident -->Cause by Change.
I tested the script can get the correct change count
Would you please advise?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2025 08:51 AM
Hi @Yanhong Huang,
(function executeRule() {
var uniqueChanges = new Set(); // Store unique Change Request numbers
var ga = new GlideAggregate('cmdb_ci_outage');
ga.addAggregate('COUNT'); // Dummy count, we will use our Set for distinct values
ga.addNotNullQuery('task_number.ref_incident.caused_by'); // Ensure there's a related Change Request
ga.query();
while (ga.next()) {
var changeNumber = ga.getValue('task_number.ref_incident.caused_by');
uniqueChanges.add(changeNumber);
}
return uniqueChanges.size; // Return distinct count
})();
Please try this script.
Thanks and Regards,
Pratap Singh Sisodia
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2025 10:52 AM - edited 03-30-2025 03:15 PM
Testing the above, it fails at line 1. for GlideAggregate reference see:
https://developer.servicenow.com/dev.do#!/reference/api/xanadu/server_legacy/c_GlideAggregateAPI
"COUNT('DISTINCT" is not valid.
Naviaget to Platform Analytics Administration -> Indicators -> Scripts to see OOB examples.
the 'ICenter.ActiveUsers' script is one example, based on that the following may work:
getCount();
function getCount() {
var ovt = new GlideRecord('cmdb_ci_outage');
//ovt.addAggregate('COUNT(DISTINCT', 'task_number.ref_incident.caused_by');
//ovt.setGroup(false);
ovt.addExtraField('task_number.ref_incident.caused_by');
ovt.addQuery('task_number.ref_incident.caused_by', '!=', '');
ovt.query();
var totalCount = ovt.getRowCount();
// gs.info(totalCount);
return totalCount;
}
I don't believe GlideAggregate supports the '.addExtraField()' method.
Testing requires knowing the Indicator definition and Indicator Source definition.