Restrict only CIs on the Change Request 'Affected CIs' tab to be added to an Outage record.

SNLearnAll
Tera Contributor

When CIs are added to the Affected CIs tab on a Change Request, they are stored on the task_ci table. This table links the CIs to the specific Change Request task. I need help querying the task_ci table, validate those CIs that have been linked to the specific Change Request task and ensure only those CIs can be inserted into the cmdb_outage_ci_mtom table where Outage record Affected CIs are stored.

 

[Summary: when adding CIs to the outage record, only allow CIs that are already listed in the Change's 'Affected CIs' tab. We want to prevent creation of outage records containing CIs not listed as affected in the change.]

 

I've tested this BR script below and others but got nothing. Any corrections to the script or alternative script to achieve the result is much appreciated.

 

(function executeRule(current, gsr) {
// Log the outage (parent) record ID
gs.log("BR Debug - Outage ID: " + current.outage, "OutageBR");

// Query the change request (task) from the outage record
var outageGR = new GlideRecord('cmdb_ci_outage');
if (outageGR.get(current.outage)) {
var changeRequestId = outageGR.task; // Assuming 'task' field stores the change request
gs.log("BR Debug - Associated Change Request: " + changeRequestId, "OutageBR");

// Query the task_ci table to find CIs linked to the change request
var taskCiGR = new GlideRecord('task_ci');
taskCiGR.addQuery('task', changeRequestId);
taskCiGR.addQuery('ci_item', current.cmdb_ci);
taskCiGR.query();

if (!taskCiGR.hasNext()) {
gs.log("BR Debug - CI not found in Affected CIs list. Aborting insert.", "OutageBR");
current.setAbortAction(true); // Prevent insert
} else {
gs.log("BR Debug - CI validated and found in Affected CIs list. Proceeding with insert.", "OutageBR");
}
} else {
gs.log("BR Debug - Outage record not found. Aborting insert.", "OutageBR");
current.setAbortAction(true);
}

})(current, gsr);
1 ACCEPTED SOLUTION

Vasantharajan N
Giga Sage
Giga Sage

@SNLearnAll - Your script logic is absolutely correct with some issue with the field names you used from Outage [cmdb_ci_outage] table and Affected CI [task_ci]. Please check with the below script 

 

  gs.log("BR Debug - Outage ID: " + current.outage.task_number, "OutageBR");

    // Query the change request (task) from the outage record
    var outageGR = new GlideRecord('cmdb_ci_outage');
    if (outageGR.get(current.outage)) {
        var changeRequestId = outageGR.task_number; // You need to use "outageGR.task_numer" instead of "outageGR.task"
        gs.log("BR Debug - Associated Change Request: " + changeRequestId, "OutageBR");

        // Query the task_ci table to find CIs linked to the change request
        var taskCiGR = new GlideRecord('task_ci');
        taskCiGR.addQuery('task', changeRequestId);
        taskCiGR.addQuery('ci_item', current.ci_item); // You need to use "current.ci_item" instead of "current.cmdb_ci"
        taskCiGR.query();

        if (!taskCiGR.hasNext()) {
            gs.log("BR Debug - CI not found in Affected CIs list. Aborting insert.", "OutageBR");
            current.setAbortAction(true); // Prevent insert
        } else {
            gs.log("BR Debug - CI validated and found in Affected CIs list. Proceeding with insert.", "OutageBR");
        }
    } else {
        gs.log("BR Debug - Outage record not found. Aborting insert.", "OutageBR");
        current.setAbortAction(true);
    }

.  


Thanks & Regards,
Vasanth

View solution in original post

2 REPLIES 2

Vasantharajan N
Giga Sage
Giga Sage

@SNLearnAll - Your script logic is absolutely correct with some issue with the field names you used from Outage [cmdb_ci_outage] table and Affected CI [task_ci]. Please check with the below script 

 

  gs.log("BR Debug - Outage ID: " + current.outage.task_number, "OutageBR");

    // Query the change request (task) from the outage record
    var outageGR = new GlideRecord('cmdb_ci_outage');
    if (outageGR.get(current.outage)) {
        var changeRequestId = outageGR.task_number; // You need to use "outageGR.task_numer" instead of "outageGR.task"
        gs.log("BR Debug - Associated Change Request: " + changeRequestId, "OutageBR");

        // Query the task_ci table to find CIs linked to the change request
        var taskCiGR = new GlideRecord('task_ci');
        taskCiGR.addQuery('task', changeRequestId);
        taskCiGR.addQuery('ci_item', current.ci_item); // You need to use "current.ci_item" instead of "current.cmdb_ci"
        taskCiGR.query();

        if (!taskCiGR.hasNext()) {
            gs.log("BR Debug - CI not found in Affected CIs list. Aborting insert.", "OutageBR");
            current.setAbortAction(true); // Prevent insert
        } else {
            gs.log("BR Debug - CI validated and found in Affected CIs list. Proceeding with insert.", "OutageBR");
        }
    } else {
        gs.log("BR Debug - Outage record not found. Aborting insert.", "OutageBR");
        current.setAbortAction(true);
    }

.  


Thanks & Regards,
Vasanth

SNLearnAll
Tera Contributor

@Vasantharajan NThank you! I applied your suggested adjustments, and the script produced the expected results.