Duplicate MRVS relationships to SC_Tasks

tilmenastij
Giga Contributor

The Business Rule “View Multi-Row Variable Sets” on the SC_Task table is creating duplicate records when a SC_Task is modified. The rule needs to be updated to check if an entry in sc_item_variables_task table between the Task and MRVS already exists.

 

Here is the script

 

 

var qList = [];

// Get variable sets related to the item
var vsr = new GlideRecord('io_set_item');
vsr.addQuery('sc_cat_item', current.request_item.cat_item.sys_id);
vsr.query();

// Get variables for each set
while (vsr.next()) {
    var vs = new GlideRecord('item_option_new');
    vs.addQuery('variable_set', vsr.variable_set);
    vs.addQuery('active', true);
    vs.addQuery('global', false);
    vs.query();

    while (vs.next()) {
        var id = vs.sys_id.toString();
        qList.push(id);
    }
}

// Push Variable Task record for each variable set
for (var i = 0; i < qList.length; i++) {
    var variableSysId = qList[i];

    // Check if a record already exists for the current variable set
    var existingRecord = new GlideRecord('sc_item_variables_task');
    existingRecord.addQuery('task', current.sys_id);
    existingRecord.addQuery('variable', variableSysId);
    existingRecord.query();

    if (!existingRecord.next()) {
        // Record doesn't exist, create a new one
        var ivt = new GlideRecord('sc_item_variables_task');
        ivt.task = current.sys_id;
        ivt.variable = variableSysId;
        ivt.insert();
    }
}
1 REPLY 1

Brad Bowman
Kilo Patron
Kilo Patron

This is a custom script.  Do you know why it is needed in your environment?  Viewing any variables on a Catalog Task is pretty basic, out of the box functionality.