De-duplication template scripted condition

maciep
Tera Contributor

Hi all,

 

I'm confused on how the condition script is applied for a de-duplication template.

 

Since the script is supposed to return an array of sys_ids of the de-duplication tasks, is the condition script used to actually assign the tasks to this template? Or does it actually do some filtering from the whole set? And if so, how? 

 

Does the condition script work in conjunction with the "Automatically select all de-duplication tasks that match the selected class" option? Because that makes it sound like the checkbox is doing the task assignment. So would the condition script even run or apply?

 

Admittedly, I stink at finding the explanatory documentation for ServiceNow. I can usually only find the "click these buttons" type of docs. So if this is explained in detail somewhere, feel free to link it to me.

 

Here is the script I'm testing with at the moment for reference (we want to ensure each dup ci in a task is in the computer class and share the same lifecycle stage/status).

 

(function() {
    // return array of task ids
    var validTasks = [];

    // loop through the tasks. We only need the sys_id of the task, since the logic is applied at the audit record
    new GlideQuery('reconcile_duplicate_task')
        .orderByDesc('sys_created_on')
        .limit(10)
        .select('sys_id')
        .forEach(function(t) {
            var sysId = t.sys_id;

            // would prefer to use the Set() object, but no can do here...so we'll use arrays instead
            // these will hold unique values across the CIs related to this task
            var lifecycleStage = [];
            var lifecycleStatus = [];
            var cmdbClass = [];


            // Get all the duplicate audit records associated with this duplication task
            // Validate their lifecycle and class values
            var grAudit = new GlideRecord('duplicate_audit_result');
            grAudit.addQuery('follow_on_task', sysId);
            grAudit.query();
            while (grAudit.next()) {
                // The duplicate_ci field of Document ID type so we're using getRefRecord
                var refRecord = grAudit.duplicate_ci.getRefRecord();

                // Get the fields we need from the duplicate_ci record
                // Add them to the appropriate array if they're not already in there
                var stage = refRecord.getDisplayValue('life_cycle_stage');
                var status = refRecord.getDisplayValue('life_cycle_stage_status');
                var className = refRecord.getDisplayValue('sys_class_name');

                if (stage && lifecycleStage.indexOf(stage) < 0) {
                    lifecycleStage.push(stage);
                }

                if (status && lifecycleStatus.indexOf(status) < 0) {
                    lifecycleStatus.push(status);
                }

                if (className && cmdbClass.indexOf(className) < 0) {
                    cmdbClass.push(className);
                }
            }

            // Now that each duplicate is proccssed, check our arrays to see if the values match across them all
            if (lifecycleStage.length === 1 && lifecycleStatus.length === 1 && cmdbClass.length === 1 && cmdbClass[0] == 'Computer') {
                validTasks.push(sysId);
            }
        });

    return validTasks;
})();

 

 

 

 

0 REPLIES 0