I'm creating a de-duplication template that use a script in the condition. The script create an array of sys_ids but it seems that the template is not using it and apply on all the de-duplicate tasks of the class instead of 20 I return in the script.
Could someone help identify what is wrong in this script?
The purpose of the script is to apply the template only on tasks with 2 CIs having a different status with one is Installed.
In the gs.info, I see the result is correct, but when I run the template it takes all the tasks.
In the template, I've activated the check box "Automatically select all de-duplication tasks that match the selected class".
Thank you
function() {
var matchingTaskSysIds = [];
// 1. Query the target de-duplication tasks
var taskGR = new GlideRecord('reconcile_duplicate_task');
taskGR.addActiveQuery(); // Condition: Task is Active
taskGR.addQuery('short_description', 'Duplicate records found in 1 class: Batch Job');
taskGR.setLimit(20);
taskGR.query();
while (taskGR.next()) {
var taskId = taskGR.getUniqueValue();
var taskNbr = taskGR.getValue('number');
var ciSysIds = [];
// 2. Find related CIs in the duplicate_audit_result mapping table
var auditGR = new GlideRecord('duplicate_audit_result');
auditGR.addQuery('follow_on_task', taskId);
auditGR.query();
while (auditGR.next()) {
var ciId = auditGR.getValue('duplicate_ci');
if (ciId) {
ciSysIds.push(ciId);
}
}
// If there are fewer than 2 CIs, they cannot have different install statuses
if (ciSysIds.length < 2) {
continue;
}
// 3. Query the actual CIs to evaluate their install_status
var ciGR = new GlideRecord('cmdb_ci');
ciGR.addQuery('sys_id', 'IN', ciSysIds.join(','));
ciGR.query();
var uniqueStatuses = {};
var statusCount = 0;
var installedCount = 0; // Track exactly how many CIs are "Installed"
while (ciGR.next()) {
var statusVal = ciGR.getValue('install_status');
// Keep track of unique status occurrences
if (!uniqueStatuses[statusVal]) {
uniqueStatuses[statusVal] = true;
statusCount++;
}
// Increment if the CI is "Installed" (value = 1)
if (statusVal === '1') {
installedCount++;
}
}
// 4. Apply final conditions:
// - CIs do NOT have the same install_status (i.e., statusCount > 1)
// - EXACTLY one CI has an install_status of "Installed" (installedCount === 1)
if (statusCount > 1 && installedCount === 1) {
matchingTaskSysIds.push(taskId);
}
}
gs.info("DeDup Job : Active Batch IDs : " + JSON.stringify(matchingTaskSysIds));
return matchingTaskSysIds;
})();