Trigger Business Rule on change of state on AVIT or VIT in Servicenow Vulnerability Response odule n

satya30
Tera Contributor

Hi, 

I have created a onChange BR on AVIT (sn_vul_app_vulnerable_item)table whenever there is a change on if state changes from closed to trigger so that each AVIt will have only one rem task in open state

But lets say when ever the assignment group on the avit i schnaged manually and saved. then avit is closed and reopened which means state is chnaged from closed - in this case my BR itself is not triggering

but if there is no change on asignment group on avit and state is changed from closed then the BR is triggerring.

Here in case after the assignment group is chnaged we are saving the form and then closing and reopening BR is not triggering. Please help me on what I can do here 

thanks,
Harika.

7 REPLIES 7

palanikumar
Giga Sage
Giga Sage

Hi @satya30 ,

 

Please share the screenshot of the conditions and Script of your BR. This will help in understanding the issue

 

Thank you,
Palani

satya30
Tera Contributor

Hi @palanikumar 

satya30_0-1760367998384.png


Script:

(function executeRule(current, previous /*null when async*/ ) {

    // // --- CASE 1: Assignment group changed ---
    // if (current.assignment_group != previous.assignment_group && current.assignment_type == "1") {
    //     var vulnGroupRule = new sn_vul.VulnerableGroupRule();
    //     vulnGroupRule.unlinkFromGroups(current);
    //     gs.info("[VIT-BR] VIT " + current.sys_id + " → unlinked from groups due to assignment change");
    // }

    // --- CASE 2: State changed ---
    gs.info(" HJ [VIT-BR] VIT ");

    var vitId = current.getUniqueValue();
    var m2m = new GlideRecord('sn_vul_m2m_vul_group_item');
    m2m.addQuery('sn_vul_vulnerable_item', vitId);
    m2m.query();

    var openTasks = [];
    while (m2m.next()) {
        var task = new GlideRecord('sn_vul_vulnerability');
        if (task.get(m2m.sn_vul_vulnerability)) {
            gs.info(" HJ [VIT-BR] VIT line 22 " + task.state + " , " + task.u_external_case_number + " , " + task.filter_type);
            if (task.state == 1 && gs.nil(task.u_external_case_number) && task.filter_type != "manual") {
                // state = Open
                gs.info(" HJ [VIT-BR] VIT line 27 ");
                openTasks.push({
                    m2mId: m2m.getUniqueValue(),
                    taskId: task.getUniqueValue(),
                    createdOn: task.sys_created_on.getGlideObject()
                });
            }
        }
    }

    if (openTasks.length > 1) {
        gs.info(" HJ [VIT-BR] VIT line 36 " + openTasks.length);
        // sort by createdOn newest first
        openTasks.sort(function(a, b) {
            gs.info(" HJ [VIT-BR] VIT line 36 " +b.createdOn+" , "+a.createdOn);
            return b.createdOn.compareTo(a.createdOn);
        });

        var keep = openTasks[0];
        gs.info(" HJ [VIT-BR] VIT line 44 " + keep);
        for (var i = 1; i < openTasks.length; i++) {
            var gr = new GlideRecord('sn_vul_m2m_vul_group_item');
            if (gr.get(openTasks[i].m2mId)) {
                        gs.info(" HJ [VIT-BR] VIT line 48");
                gr.deleteRecord();
            }
        }
        //  gs.info("[VIT-BR] VIT " + vitId + " → kept task " + keep.taskId + ", unlinked " + (openTasks.length-1) + " older tasks");
    }


})(current, previous);

 

Hi,

 

The condition highlighted in the below line may not work in all the scenarios. You can remove this check and see whether your issue is resolved

if(task.state ==1&& gs.nil(task.u_external_case_number) && task.filter_type !="manual") {

 

Thank you,
Palani

I just wanted to push only open rem tasks then in that case how should I be able to look into it.

thanks,
Harika