List collector field to assign the configuration_item and insert task_ci records for sys_id.

Pushpanjali3
Tera Contributor

Question:
if cmdb_cis list collector has only 1 sys_id put it into the field configuration_item 

if cmdb_cis list collector has more than 1 sys_id, put the first into the field configuration_item 

and create records to each sys_id in the table task_ci for the same RITM.

 

FYI: Using catalog item with list collector field: equipment_choice which has 3 choices.

I have created below "before" Business rule but no luck. Please help on to debug

BR:

(function executeRule(current, previous /*null when async*/) {
  // Replace with the actual SysID of the "Manufacturing" category
    var manufacturingCategorySysID = '46ea1c3f8738f51025a1b95e8bbb35a9';

    // Check if the catalog item belongs to the 'Manufacturing' category using SysID
    if (current.cat_item.category == manufacturingCategorySysID) {
               // Set field values based on catalog item variables if available
        if (current.variables.request_for) {
            current.requested_for = current.variables.request_for;
            current.u_requested = current.variables.request_for;
        }

        if (current.variables.location) {
            current.location = current.variables.location;
        }

        if (current.variables.sc_category) {
            current.u_category_task = current.variables.sc_category;
        }

        if (current.variables.watch_list) {
            current.watch_list = current.variables.watch_list;
        }

        if (current.variables.technician_name) {
            current.work_notes_list = current.variables.technician_name;
        }

        if (current.variables.description) {
            current.description = current.variables.description;
        }

        if (current.variables.estimated_end) {
            current.estimated_delivery = current.variables.estimated_end;
        }

        if (current.variables.start_reservation) {
            current.work_start = current.variables.start_reservation;
        }

        if (current.variables.end_reservation) {
            current.work_end = current.variables.end_reservation;
        }

        // Handle cmdb_cis list collector logic
        var cmdbCIs = current.variables.equipment_choice; //variable name for the list collector
        if (cmdbCIs) {
            var ciArray = cmdbCIs.split(',');
            var ciCount = ciArray.length;

            // Set the first CI in the configuration_item field if available
            if (ciCount > 0) {
                current.configuration_item = ciArray[0]; // Assign the first sys_id to configuration_item
            }

            // Create a record in task_ci for each sys_id
            for (var i = 0; i < ciCount; i++) {
                var taskCiGr = new GlideRecord('task_ci');
                taskCiGr.initialize();
                taskCiGr.task = current.sys_id; // Link to the current RITM
                taskCiGr.ci_item = ciArray[i]; // Set the CI sys_id
                taskCiGr.insert();
            }
        }

    } else {
        gs.info('BR RITM skipped for non-Manufacturing category: ' + current.number);
    }


   
})(current, previous);




1 REPLY 1

Brad Bowman
Kilo Patron
Kilo Patron

Is your BR running before Insert on the sc_req_item table? Are the RITM fields getting set from the variables?  When you set the value of the configuration_item field, that record should be added as an Affected CI, so you might want to start your loop at i=1 if you are getting duplicates once this is working.  I don't see anything glaring in your script.  Add some more gs.info lines for troubleshooting so that you can confirm it is executing, and see how far the script is getting, the if conditions that are met, etc.