Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Create rule to delete child VSTs when a VSR is deleted

tilmenastij
Giga Contributor

When a Vulnerability Scan  is deleted, we want a business rule to also delete any Vulnerability Tasks  with the Deleted Vulnerability Scan referenced in the ‘Vulnerability scan’ field of theVulnerability task

 

 

 

Here is the business rule:

 

 

(function executeRule(current, previous /*null when async*/) {
    // Create array
    var qList = [];

    // Get variable sets related to 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);
        }
    }

    // Update existing records
    for (var i = 0; i < qList.length; i++) {
        var ivt = new GlideRecord('sc_item_variables_task');
        ivt.addQuery('task', current.sys_id);
        ivt.addQuery('variable', qList[i]);
        ivt.query();

        if (ivt.next()) {
            // Update existing record
            ivt.variable = qList[i];
            ivt.update();
        }
    }
})(current, previous);
0 REPLIES 0